0

I am new to C# and SQL Server 2014 Express and I am using Windows forms.

I am building the small application which read/write from/to SQL Server database. I am trying to automatically attach SQL Server database, then read data from a table named Test_Table and then fill a Datagridview.

The code I used:

 SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

 SqlCommand MyCommand= new SqlCommand();

 DataTable DataTable = new DataTable();

 SqlDataAdapter Sql_Data_Adapter= new SqlDataAdapter();

 private void button1_Click(object sender, EventArgs e)
 {
        MyConnection.Open();
        MyCommand.CommandText = "SELECT * FROM Test_Table ";
        MyCommand.Connection = MyConnection;

        Sql_Data_Adapter.SelectCommand = MyCommand;
        Sql_Data_Adapter.Fill(DataTable);

        dataGridView1.DataSource = DataTable;

        MyCommand.Parameters.Clear();
        Sql_Data_Adapter.Dispose();
        MyConnection.Close();
}

App.config:

<connectionStrings>  
   <add name="MyConnectionString"        
        connectionString="Data Source=localhost; AttachDbFilename=D:\\DB\\MyDB.mdf;Integrated Security=True"/>
</connectionStrings> 

When I click on button1 it throws an error:

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

I have no other databases attached and the database path is correct. I had a look here, here and here but non of them helped.

Any idea how can I automatically attach a SQL Server database file? Thank you

4
  • 1
    The answer is right there. Check it's not on a UNC share. Check the SQL Server account has "Full Access" to the mdf file and any associated ldf files. Alternatively consider using "localdb" to access the data, as it exists for this sort of scenario.. Commented May 20, 2017 at 11:02
  • Never, Never use AttachDbFilename with SQL Server. The server owns the mdf file. You do not have the credentials to get database by file name. Instead use the database name. See www.connectionstrings.com. Commented May 20, 2017 at 11:14
  • @ Ben . I have checked it and it has full acces to the database. Commented May 20, 2017 at 11:16
  • jdweng@ ok , but what do you exactly mean by "Instead use the database name"? I just want an automatic attach. Commented May 20, 2017 at 11:17

1 Answer 1

1

What is the version of SQL Server are you using? This will explain a lot about your error message.

Your Connection String:

"Data Source=localhost; AttachDbFilename=D:\DB\MyDB.mdf;Integrated Security=True"

You can only use AttachDbFilename with SQL Express. In your connection string, the Data Source is set to localhost. That sounds like you have a full version SQL Server installed.

If you have a full version of SQL Server installed, you can only attach a database via Management Studio.

If you are using SQL Express, change your data source:

Data Source=.\SQLEXPRESS 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I updated my question, yes I use MS SQL express.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.