1

I'm new to C# and don't know much!

I have created a local database in C# 2012 and want to make a connection to it. I tested connection with its wizard and it said successfully connected to database.

So I copied connection string address to my code but after few seconds of running i have and exception error!

I don't know where the problem is!

Here is my code:

System.Data.SqlClient.SqlConnection myConnection;

private void Form1_Load(object sender, EventArgs e)
{
    myConnection = new System.Data.SqlClient.SqlConnection();
    myConnection.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\jack\\Documents\\Visual Studio 2012\\Projects\\dictionary\\dictionary\\dictionaryDb.mdf;Integrated Security=True;Connect Timeout=30";
    myConnection.Open();

    MessageBox.Show("successfully connected!");
}

This is the exception error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

And my other question is: is it differences between SQL Server Express and local database in C#?

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saeid
  • 448
  • 1
  • 7
  • 19

1 Answers1

3

It seems like you miss one backslash \ between (LocalDB) and v11.0:

myConnection.ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\jack\\Documents\\Visual Studio 2012\\Projects\\dictionary\\dictionary\\dictionaryDb.mdf;Integrated Security=True;Connect Timeout=30";

One back slash will be consider as escape character, two backslashes \\ will be treated as backslash. Alternative, do this (using @) to make your string character clearer:

myConnection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\jack\Documents\Visual Studio 2012\Projects\dictionary\dictionary\dictionaryDb.mdf;Integrated Security=True;Connect Timeout=30";

You could also specify your connectionString in your app.config <connectionStrings> <add name="DefautConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\jack\Documents\Visual Studio 2012\Projects\dictionary\dictionary\dictionaryDb.mdf;Integrated Security=True;Connect Timeout=30;" .. and so on which is not uncommon.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • oh thank you Ian. i thought double backslash is just for address! – Saeid Apr 02 '16 at 15:54
  • it works now :) is it difference between local database and sql express? – Saeid Apr 02 '16 at 15:55
  • @Saeid local data base means that your connection is to local machine (the PC you are using). SQL express is the name of the database, not the machine that hosts the database. Actually the full name is `SQL Server Express`, the database name is `SQL Server` – Ian Apr 02 '16 at 15:56
  • i cant use my code on other machines by local database? or i can atach it to my program?! :p – Saeid Apr 02 '16 at 16:00
  • @Saeid yes, if you do not specify different connection on other machine, because for the other machine, your database is not in the local machine. And... that is also the reason why people put connection string in the `app.config` (or `web.config`) instead of in the code so that when they deploy their application *in other machine* but want to use the same database, they only need to change their connection string in the `app.config` (or `web.config`) - they *do not* need to change their code. They cannot do this if the connection string is hardcoded (like what you do) – Ian Apr 02 '16 at 16:03