0

I'm not exactly sure why this error was occurring I was assuming it was because of my App.Config configuration manager connection string I've tried dabbling with it but nothing seems to work.

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)

My app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="fluxConnection"
             connectionString="Data Source=.;Initial Catalog=database; Integrated Security=SSPI;User ID=root;Password=;" 
             providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Code:

  private void metroButton2_Click(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["fluxConnection"].ConnectionString;

    SqlConnection con = new SqlConnection(connStr);
    con.Open();

    if (con.State == ConnectionState.Open) {
        MessageBox.Show("Connection Successful");
    }
    if (con.State == ConnectionState.Closed)
    {
        MessageBox.Show("Connection is Closed");
    }
    if (con.State == ConnectionState.Broken)
    {
        MessageBox.Show("Connection is Broken");
    }

    con.Close();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vince
  • 23
  • 4
  • 2
    Check the connection string; you have not specified the `Source` – sujith karivelil Dec 30 '16 at 07:50
  • 2
    https://www.connectionstrings.com/ – Dmitry Bychenko Dec 30 '16 at 07:50
  • The error states that there was no way to open a named pipe to the server in question, is the protocol named pipes enabled for the server? – Lasse V. Karlsen Dec 30 '16 at 08:01
  • I'm currently using xamp phpmyadmin for the database not the sql server that vs provides – Vince Dec 30 '16 at 08:03
  • Your original question had MySQL tags. Not sure why others removed those, but it seems clear to me you are using SQL Server code to connect to MySQL. See the suggested site of Dmitry to see what connection string you need. Also, you need [Connector/Net](https://dev.mysql.com/downloads/connector/net/6.9.html) from MySQL to work with MySQL databases. – Patrick Hofman Dec 30 '16 at 08:05
  • [Related](http://stackoverflow.com/q/21618015/993547). – Patrick Hofman Dec 30 '16 at 08:06
  • 1
    You've defined `Integrated Security=SSPI` **and** `User ID=root;Password=` in your connection string - this won't work - you need to make up your mind. **EITHER** use the "integrated security" to use the current Windows principal to connect to the SQL Server database, **OR** alternatively specify a specific user id and password - but **DO NOT** have both in there! – marc_s Dec 30 '16 at 08:08

1 Answers1

0

I was rather confused. All I did was add Mysql to my reference and did the according connectionstring for mysql and was able to solve my issue.

Vince
  • 23
  • 4