Reputation: 103
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.
Upvotes: 0
Views: 4365
Reputation: 2293
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.
Upvotes: 1