an attempt to attach an auto-named database c#

hamed algazaly 106 Reputation points
2021-09-10T15:07:55.973+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,318 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,626 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2021-09-13T03:20:13.23+00:00

    @hamed algazaly ,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.


3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,181 Reputation points
    2021-09-10T15:27:27.803+00:00

    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. P a u l 10,496 Reputation points
    2021-09-10T17:02:41.58+00:00

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


  3. Erland Sommarskog 106.5K Reputation points
    2021-09-12T14:25:30.937+00:00

    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
    
    0 comments No comments