question

hamedalgazaly-0500 avatar image
0 Votes"
hamedalgazaly-0500 asked JackJJun-MSFT answered

an attempt to attach an auto-named database c#

hello everyone..
my C# application with .mdf database works fine in my development machine ..
but when I try to run the program an another machine I receive this error message..

an attempt to attach an auto-named database for file <my development machine .mdf path> failed.
a database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

this is my connection String
<add name="conn" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\mr-ho\source\repos\HighTopClinic\HighTopClinic\clincDB.mdf;Integrated Security=True"
providerName="System.Data.sqlclient" />

I'm using SqL LocalDB 2019


please help


sql-server-generaldotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@hamedalgazaly-0500,you could try the following steps to avoid the error you get when you access the database in another machine.

First, Please place your mdf file in your Winformapp/bin/debug folder.

Second, you could set the connection string by using DataDirectory in the App.Config.

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <startup> 
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
     </startup>
   <connectionStrings>
     <clear />
     <add name="conn" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\School.mdf;Integrated Security=True"
 providerName="System.Data.sqlclient" />
   </connectionStrings>
     
 </configuration>

Based on my test, I can connect the database successfully when I run the app in another computer.

Best Regards,
Jack



If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.








5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

so on the other machine does

dir C:\Users\mr-ho\source\repos\HighTopClinic\HighTopClinic\clincDB.mdf

list a file and is it read/write. also check permissions.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This path is in my development machine but is not exist in the other machine

0 Votes 0 ·
Bruce-SqlWork avatar image Bruce-SqlWork hamedalgazaly-0500 ·

a local db must be exist on the machine that will attach, no shares allowed. you need to copy the mdf to the other machine, and update the connection string to the path.

0 Votes 0 ·
Paul-5034 avatar image
0 Votes"
Paul-5034 answered hamedalgazaly-0500 commented

Is the other machine a developer machine, or is it for a QA/staging/production environment?

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just my brother pc that I'm trying to run the application on..

If it work in that pc that mean i can run it on any other pc

0 Votes 0 ·
Paul-5034 avatar image Paul-5034 hamedalgazaly-0500 ·

Is it required that you use an mdf file rather than you just hosting a SQL server instance and your brother's PC connects to it?

0 Votes 0 ·

It's an mdf file attached in my project

0 Votes 0 ·
Show more comments
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

It seems that you want the same database to be accessible from the two different machines. Then you cannot use LocalDB which is only for local database. You need to install SQL Server Express. The connection string should not have this funny AttachDBFilename, but you simply create and set up the database within your instance, and the conntiction string should read:

Server=YOURMACHINE\SQLEXPRESS;Initial Catalog=YourDB;Integrated Security=SSPI
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.