0

Don't Mark it as Duplicate or On Hold. I have tried all the solutions given in these answers here 1, 2 , 3 and 4. Following is my piece of code.

  string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='D:\Codes\Test\MyDataBase.mdf';Integrated Security=True;";
  SqlConnection con = new SqlConnection(connectionString);
  string query = "select * from StudentInfo";
  SqlCommand cmd = new SqlCommand(query, con);

  con.Open();   //Exception Appears Here...

  SqlDataReader dr = cmd.ExecuteReader();
  if (dr.HasRows)
  {
      while (dr.Read())
      {
           Console.WriteLine(dr[0] + "  " + dr[1] + "   " + dr[2] + "  " + dr[3]);
       }
  }
  con.Close();

Following Exception appears when I open the connection:

System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file MyDataBase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'

I added a Service-Based Database in my Project made in Visual Studio 2017 Version 15.6.4. I have tried many solutions from different websites too , Does its solution really exits? or we have to make a new project and start from scratch again?

DrHouseofSQL
  • 550
  • 5
  • 16
MHS
  • 1
  • 5
  • If it's a local database, why do u need to add it as a `Service-Based Database` ?? – Software Dev Mar 30 '18 at 00:46
  • [This documentation](https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#initialcatalog) suggests that the lack of an initial catalog is causing the path to be used as the database name, and your localdb already has an instance of this. Try adding `Initial Catalog=this-is-a-test;` to the connection string and see if that alleviates the error, then name it something more appropriate for your application. – Jonathon Chase Mar 30 '18 at 00:48
  • @JonathonChase I tried Initial Catalog=this-is-a-test; gives me another exception. **System.Data.SqlClient.SqlException: 'Cannot attach the file 'D:\Test\MyDataBase.mdf' as database 'this-is-a-test'.'** – MHS Mar 30 '18 at 00:54
  • Have you tried removing the single-quotes from your connection string? – Jonathon Chase Mar 30 '18 at 00:59
  • @zackraiyan Because in VS2017 Test->Right Click->Add->Data , Only `Service-Based DataBase` option is available. Other option includes DataSet , ADO.net Entity Data Model. – MHS Mar 30 '18 at 01:00
  • just change ur connectionstring as follows `@"Data Source=.\SQLEXPRESS; AttachDbFilename=D:\Codes\Test\MyDataBase.mdf Integrated Security=True; Connect Timeout=30; User Instance=True"` – Software Dev Mar 30 '18 at 01:03
  • @JonathonChase Yes I have tried removing single quotes but still the same exception appears. – MHS Mar 30 '18 at 01:05
  • @zackraiyan I have tried this `@"Data Source=.\SQLEXPRESS; AttachDbFilename=D:\Codes\Test\MyDataBase.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True"` but it gives me another exception **System.Data.SqlClient.SqlException: 'The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.'** – MHS Mar 30 '18 at 01:06
  • That string is missing the semicolon between the AttachDbFilename section and the Integrated Security section. – Jonathon Chase Mar 30 '18 at 01:09
  • just remove `user interface=true` – Software Dev Mar 30 '18 at 01:09
  • @JonathonChase I mistakenly missed the semicolon but its written in my project. – MHS Mar 30 '18 at 01:12
  • @zackraiyan Tried removing `user interface=true` , the same exception appears which I have asked in the question. – MHS Mar 30 '18 at 01:13
  • Is stopping/deleting/creating/starting the localdb an option? – Jonathon Chase Mar 30 '18 at 01:17
  • @JonathonChase If we create a new project with a new Db, then it will work but creating whole database with multiple tables plus if you are having a mvc or a n-tier architecture will be much time consuming :-( . – MHS Mar 30 '18 at 01:21
  • hey waitt.....is ur visual studio is of express version ? – Software Dev Mar 30 '18 at 01:26
  • @zackraiyan Nope, its **Visual Studio 2017 Community Version 15.6.4**, downloaded from here [link](https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel=15). – MHS Mar 30 '18 at 01:30
  • Given that you have tried many of the common workarounds, I think that your error is most likely tied to the first part of the error, do you have another attached database Named MyDatabase by chance? – Mitchel Sellers Mar 30 '18 at 02:06
  • @MitchelSellers I have already checked the **Sql Server Folder located in Program Files in C Disk**. There's no similar Database. – MHS Mar 30 '18 at 17:26
  • **I think its solution not exists after all.** – MHS Mar 31 '18 at 15:00

0 Answers0