none
Error attaching db file with VBX LINQ to SQL RRS feed

  • Question

  • I am trying to find the source of the error:

    Message="An attempt to attach an auto-named database for file \Sbear\Medical\Diabetes\DiabetesData2.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

     

    I am using VB Express 2008 and LINQ to SQL. Not sure which forum would be best. Any suggestions?

     

    Thanks,

    --Mable

     

    Thursday, March 6, 2008 6:08 AM

Answers

  • Caddre--

     

    I'm not sure I understand all this. I have several simple apps that mimic the examples in the Help files, letting the designer generate the SQL database and the connection string. The designer-generated connection strings all have UserInstance=TRUE, and they work fine.

     

    In any case, I remembered something I read some time ago. If you specify "Database=..." in the connection string, you cannot also specify "AttachDBFilename=..." So I tried:

    dc.Connection.ConnectionString = Database=DiabetesData;Data Source=.\SQLExpress;Integrated Security=True;User Instance=True

    It seems to be working.

     

    Thank you for giving me several links regarding connection strings. I will continue to try to sort out their behaviour. In the meantime, I think I have something that works.

     

    --Mable

    Thursday, March 6, 2008 10:40 PM

All replies

  • Try the thread below for the reason for your error it is related to your connection string.

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=989092&SiteID=1

     

    Thursday, March 6, 2008 1:24 PM
  • Thanks for the suggestion, but I tried several different connection strings and still have not been successful. Here is a little more detail on what I'm trying to do.

     

    I have an existing database. I can open it using SQL Management Express running as a User Instance.

     

    In my VBE 2008 program, I first open a DataContext:

    Dim dc As New DiabetesData(strConnection)

     

    Then I examine the metadata:

     For Each mt As MetaTable In dc.Mapping.GetTables

    ...(examine some properties)...

    Next

     

    This much works as I expect. The problem comes later when I execute this code:

    Debug.Print("dc.Connection.ConnectionString = " & dc.Connection.ConnectionString)

    frmEZView.bsPrimary.DataSource = dc.GetTable(Of BGTest)()

     

    (bsPrimary is a BindingSource on a form named frmEZView.

    Public Class DiabetesData includes a property Public BGTests As Table(Of BGTest) and a subsequent definition of BGTest.)

     

    I tried each of the following values for strConnection:

    dc.Connection.ConnectionString = Data Source=.\SQLExpress;Integrated Security=True;User

    Instance=True;AttachDBFilename=\Sbear\Medical\Diabetes\DiabetesData2.mdf

     

    dc.Connection.ConnectionString = Data Source=.\SQLExpress;Integrated

    Security=True;AttachDBFilename=\Sbear\Medical\Diabetes\DiabetesData2.mdf

     

    dc.Connection.ConnectionString =

    AttachDBFileName=\Sbear\Medical\Diabetes\DiabetesData2.mdf;Server=localhost\sqlexpress;Integra

    ted Security=SSPI;User Instance=true;MultipleActiveResultSets=true

     

    dc.Connection.ConnectionString = AttachDBFileName="\Documents and Settings\Mary\BearsData\Sbear\Medical\Diabetes\DiabetesData2.mdf";Server=localhost\sqlexpress;Integrated Security=SSPI;User Instance=true;MultipleActiveResultSets=true

     

    dc.Connection.ConnectionString = Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=\Documents and Settings\Mary\BearsData\Sbear\Medical\Diabetes\DiabetesData2.mdf

     

    Each of these resulted in the error:

    An attempt to attach an auto-named database for file \Sbear\Medical\Diabetes\DiabetesData2.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


    Finally, I renamed the db using MSMS: Name=DiabetesData and tried the ConnectionString:

    dc.Connection.ConnectionString = Database=DiabetesData;Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=\Documents and Settings\Mary\BearsData\Sbear\Medical\Diabetes\DiabetesData2.mdf

     

    This changed the error message:

    Database 'C:\Documents and Settings\Mary\BearsData\Sbear\Medical\Diabetes\DiabetesData2.mdf' already exists. Choose a different database name. Cannot attach the file '\Documents and Settings\Mary\BearsData\Sbear\Medical\Diabetes\DiabetesData2.mdf' as database 'DiabetesData'.

     

    I know the database already exists. How do I tell VBE to use one of its tables as a DataSource, not try to create a new database?

     

    Thanks,

    --Mable

    Thursday, March 6, 2008 7:28 PM
  • You cannot use User instance connection string because LINQ will create a new database when you use the User instance in your connection string because it is a single user database. Solution make a copy server instance and connect with a new connection string User instance false. Now I am not sure if VB Express will let you do that because it is free but I know in VS2008 standard and above it is easy. Here is a blog post with the problem. I am assuming you know the restrictions and limitations of user instance which is not applicable to SQL Server Express server instance databases.

     

    http://blog.benhall.me.uk/2007/08/visual-studio-2008-beta-2-sample-code.html

     

    Thursday, March 6, 2008 8:35 PM
  • Caddre--

     

    I'm not sure I understand all this. I have several simple apps that mimic the examples in the Help files, letting the designer generate the SQL database and the connection string. The designer-generated connection strings all have UserInstance=TRUE, and they work fine.

     

    In any case, I remembered something I read some time ago. If you specify "Database=..." in the connection string, you cannot also specify "AttachDBFilename=..." So I tried:

    dc.Connection.ConnectionString = Database=DiabetesData;Data Source=.\SQLExpress;Integrated Security=True;User Instance=True

    It seems to be working.

     

    Thank you for giving me several links regarding connection strings. I will continue to try to sort out their behaviour. In the meantime, I think I have something that works.

     

    --Mable

    Thursday, March 6, 2008 10:40 PM