0

I am trying to connect to my local database(Sql Express) in Visual Studio to the Application Forms button. In Server Explorer and properties, connection string is:

  connectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\Work\Documents\Visual Studio 2015\Projects\MyTest\MyTest\Database.mdf";Integrated Security=True;Connect Timeout=30";

All the guides I've been reading use following connection string:

 connectionString = "Data Source=localhost\\SQLExpress;Initial Catalog=Databasen;User ID=admin;Password=password";

But when inserting this string path in my button in Form1, and press button, an error appears - couldn't open connectionSystem.Data.SqlClient.SqlException (0x80131904):

Could someone explain the difference and which one of them should be used and why? And how i'm able to use the last connection example.

5
  • 1
    The first connection string uses a database file MDF through the LocalDB libraries while the second one tries to use a database handled by the Sql Server Express service. This last one requires installation of Sql Server Express and the appropriate steps to configure the server and create the database. Do you have Sql Server Express installed and running? Do you have setup the database named Databasen? Commented Apr 9, 2016 at 22:55
  • I have added Microsoft SQL Server Data Tools in Visual Studio, this cover Sql Server Express, right? When expand Data Connections in Server Explorer, I'll find my established database - Databasen.mdf Commented Apr 9, 2016 at 23:28
  • Is there anyone who can advise me? Can't do anything until the connection string is OK.... Commented Apr 10, 2016 at 14:59
  • The first connectionstring is wrong because you don't have doubled the backslash. Or try just adding a @ before the string Commented Apr 10, 2016 at 15:02
  • Also the double quotes around the file name could be replaced by single quotes @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\Work\Documents\Visual Studio 2015\Projects\MyTest\MyTest\Database.mdf';Integrated Security=True;Connect Timeout=30"; Commented Apr 10, 2016 at 15:05

1 Answer 1

1

The top one is for a SQL data file connect that's located here: C:\Users\Work\Documents\Visual Studio 2015\Projects\MyTest\MyTest\Database.mdf

connectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\Work\Documents\Visual Studio 2015\Projects\MyTest\MyTest\Database.mdf";Integrated Security=True;Connect Timeout=30";

The second is to a local database. Make sure the name of the sever "localhost" is correct. Also I noticed in your database name you have 'Databasen' is this correct spelling. Check user name and password too.

connectionString = "Data Source=localhost\\SQLExpress;Initial Catalog=Databasen;User ID=admin;Password=password";

Please explain what you are doing when you say

But when inserting this string path in my button in Form1, and press button, an error appears - couldn't open connectionSystem.Data.SqlClient.SqlException (0x80131904)

You should never put the connection string in your user interface.

If you need to know how to connect through code we can show you.

Sign up to request clarification or add additional context in comments.

13 Comments

Been spending way too long time with this issue.. I am not sure where to put the connection string. private void button1_Click(object sender, EventArgs e) { string connectionString = null; SqlConnection cnn; connectionString ="Data Source=localhost\\SQLExpress;Initial Catalog=Databasen;User ID=admin;Password=password"; cnn = new SqlConnection(connectionString); try { cnn.Open(); MessageBox.Show("Connection were successfull!"); }
I haven't added any password actually. Where can I modify this and set up root? Everyone else set up, seems to be so different compare to mine... Thanks for your answers btw!
The connection string goes in web config. This video tutorial should help youtube.com/watch?v=SeIeOp_XmF4. . Also you should check out tutorials on asp.net site . Lots of good stuff
Thanks for the link! However, I am not using web application. But tried to change SqlConnection connection = new SqlConnection("server = localhost; user ID=username; password=password; database=Databasen"); Error message - Cant open connection to SQL Server / --> Can't find path. So I changed to SqlConnection connection = new SqlConnection("server = localhost; uid=test; lid=password; database=Databasen"); The error message is: Keyword can't be supported: lid.
Can you open database in SQL management studio? What the the name of the server when you connect?
|

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.