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.

Global
  • 103
  • 3
  • 13
  • 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? – Steve Apr 09 '16 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 – Global Apr 09 '16 at 23:28
  • Is there anyone who can advise me? Can't do anything until the connection string is OK.... – Global Apr 10 '16 at 14:59
  • The first connectionstring is wrong because you don't have doubled the backslash. Or try just adding a `@` before the string – Steve Apr 10 '16 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";_ – Steve Apr 10 '16 at 15:05

1 Answers1

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.

haydnD
  • 2,225
  • 5
  • 27
  • 61
  • 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!"); } – Global Apr 09 '16 at 23:32
  • 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! – Global Apr 09 '16 at 23:36
  • The connection string goes in web config. This video tutorial should help https://www.youtube.com/watch?v=SeIeOp_XmF4. . Also you should check out tutorials on asp.net site . Lots of good stuff – haydnD Apr 09 '16 at 23:42
  • 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. – Global Apr 10 '16 at 00:23
  • Can you open database in SQL management studio? What the the name of the server when you connect? – haydnD Apr 10 '16 at 00:50
  • I don't think I have it installed - only added Microsoft SQL Server Data Tools when installed Visual Studio. The name of the server is Anoto, (my Microsoft login name) The name of the Data Connections is Databasen.mdf Am I missing something important? – Global Apr 10 '16 at 01:15
  • Change 'localhost' to server name. Are you able to connect to database in visual studio withe the SQL tools? – haydnD Apr 10 '16 at 01:18
  • I changed from localhost to Anoto. Still get the error - Keyword can't be supported "lid". Where do I found the actually SQL tools? When I choose Databasen.mdf and modify connection, then Microsoft SQL Server Database File (SqlClient), database file name: C://xx/databasen.mdf, then press test connect. The result is Test connection succeeded. – Global Apr 10 '16 at 01:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108728/discussion-between-global-and-ironman99). – Global Apr 10 '16 at 02:01
  • start from the beginning. make sure you download sql management studio: https://encrypted.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj2uvfqoIfMAhWHwYMKHb0_CZAQFggfMAA&url=https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fdownload%2Fdetails.aspx%3Fid%3D7593&usg=AFQjCNHAiQPOsRoJFQubNm5H_oFC9kNg0w&sig2=SqF6f011_agN-oYFh7YoUg .... then connect to the local server: http://stackoverflow.com/questions/360141/how-to-connect-to-local-instance-of-sql-server-2008-express:.... then connect to your db from app: https://msdn.microsoft.com/en-us/library/ms171890.aspx – haydnD Apr 11 '16 at 18:57
  • Ille try it as soon as I can. Will other users be able to run the project in Visual Studio and connect to the database if zip down the whole project? Or do they need to change path etc? – Global Apr 12 '16 at 05:35
  • If you are wanting to have it reusable - with a db - then either you'll need to provide the SQL scripts to creat db or maybe easier for you would be to use a SQL data file that can be zipped with your solution and reused. https://msdn.microsoft.com/en-us/library/ms233763.aspx. (Instructions on how) – haydnD Apr 12 '16 at 21:23
  • Now it suddenly happens again... Can't understand what's wrong. I am refering to the App.config string " connectionString="Data Source=(LocalDB\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Databas.mdf;Integrated Security=True;Connect Timeout=30" and put it into my connection string as string connection ="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Databas1.mdf;Integrated Security=True;Connect Timeout=30" Recieve the error (0x80131904) again.. What to do? – Global Apr 30 '16 at 19:55