sandorfalot
sandorfalot

Reputation: 1062

Connecting to SQL Server in Visual C#

I'll show the code I'm using, but I can't connect to SQL Server through Visual C#. I have installed what came in the C# package and I keep getting this error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: 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)

I can't figure out how to connect to the server. I'm using Windows 8, SQL Sever 2012 (Visual Studio 2012) and if I go to connect in the top toolbar, it doesn't matter if I use (local), localhost or my IP address. I can't figure this out and its driving me nuts! Here's my code, if it helps.

private void btnSave_Click(object sender, EventArgs e)
{
    System.Data.SqlClient.SqlConnection sqlConnection1 =
         new System.Data.SqlClient.SqlConnection("Server=(local);Database=Database1DataSet;Trusted_Connection=True;");

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT VALUES (id, Medname, MedDose, MedAm, MedNoon, MedEve, MedNight, MedPRN, MedOther) VALUES (1, 'Clonazepam', '2mg', 'true', 'true', 'false', 'true', 'true', 'false')";
    cmd.Connection = sqlConnection1;

    sqlConnection1.Open();
    cmd.ExecuteNonQuery();
    sqlConnection1.Close();
}

Thanks so much. This is my first time using SQL Server through Visual Studio.

Upvotes: 1

Views: 6660

Answers (1)

Solomon Rutzky
Solomon Rutzky

Reputation: 48826

If you installed SQL Server as part of the Visual Studio 2012 install process, then it installed SQL Server Express LocalDB. LocalDB is very lightweight and does not have a constantly running service. If you actually did install LocalDB while installing Visual Studio, you should be able to see the "instance" by going to the View menu in Visual Studio and selecting SQL Server Object Explorer. There should be a folder (so to speak) for "SQL Server" and expanding that should show you something along the lines of:

(localdb)\V11
or
(localdb)\Projects

Whatever that value is, THAT is what you specify for the "server=" in the connection string. So, your connection string should look more like this (due to the backslash, you should prefix the string with @):

@"Server=(localdb)\V11;Database=Database1DataSet;Trusted_Connection=True;"

Also, when you create a new project in Visual Studio, it should pre-populate the correct connection string in the Debug tab of Project Properties.

EDIT:
Also, you can find the instance name(s) in the following location as each instance will be a subfolder:

C:\Users\{YourUserName}\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances

Upvotes: 1

Related Questions