0

I get this error when trying to connect to SQL Server:

System.Data.SqlClient.SqlException: '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 wish I could be more specific with what I am doing but I'm so confused. All I can say is that I am trying to write data to a SQL Server database. This is my code:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("data source = DESKTOP-4HV953I; initial catalog= VyPr-Logins");

    SqlCommand cmd = new SqlCommand("sp_insert", con);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@Username", textBox1.Text);
    cmd.Parameters.AddWithValue("@Password", textBox2.Text);

    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();

    if (i != 0)
    {
        MessageBox.Show(i + "Data Saved");
    }
}
6
  • 4
    Are you able to connect to the database server from sql management studio ? Commented Dec 10, 2017 at 5:54
  • check out this link for Connection Strings in C# it gives examples for all databases just click sql-server when you go here.. also learn to use the App.Config or Web.Config C# Database Connection Strings Commented Dec 10, 2017 at 5:56
  • 1
    also change this code to execute inside a try{}catch(){} int i = cmd.ExecuteNonQuery(); just use cmd.ExecuteNonQuery If you are confused you can always do a google search plenty of simple easy to follow examples online Commented Dec 10, 2017 at 6:00
  • 1
    Are you sure your sql server instance is running and is able to accept connections? (by default it can usually work by named-pipes, but not TCP/IP, though named pipes should work if DESKTOP-4HV953I is the name of the machine you're running on). Commented Dec 10, 2017 at 6:04
  • Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all! Commented Dec 10, 2017 at 8:33

1 Answer 1

1

SQL Server defaults to not allowing remote connections, you have to go into SQL Server Configuration Manager and enable remote access for the protocol of interest.

With prior versions of SQL Server the configuration manager was added to the start menu but that does not appear to be the case for my current 2014 install, I had to run mmc and add the SQL Server Configuration Manager add-in myself.

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

1 Comment

That's only the Express edition! All other editions do allow remote connections out of the box

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.