1

I run this function on my asp.net website:

private static DataTable GetData(string query)
{
    string strConnString = "SERVER=localhost;DATABASE=database;UID=root;PASSWORD=123456789;";
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = query;
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

With this query:

"select * from Client where id='"+ customerId +"'"

And i get this Exception:

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)

Any idea what can fix this?

6
  • Can you connect to the database using a program like MySQLWorkbench or HeidiSQL? If so, does the query (when customerId is replaced with actual data) execute correctly? Commented Oct 24, 2013 at 22:59
  • 2
    SqlConnection is used to connect o MS SQL Server, not to mysql Commented Oct 24, 2013 at 23:03
  • @IlyaBursov you could write the answer then.... Commented Oct 24, 2013 at 23:12
  • 1
    stackoverflow.com/questions/18253120/… Commented Oct 24, 2013 at 23:20
  • Is the asp.net code running on the same machine as the SQL Server (localhost)? Commented Oct 25, 2013 at 0:10

1 Answer 1

1

Connecting with Mysql, try this:

*Add Mysql.Data.dll, read here

*Add this line on top of your page

using MySql.Data.MySqlClient;

And try this edited method:

private static DataTable GetData(string query)
{
    string strConnString = "SERVER=localhost;DATABASE=database;UID=root;PASSWORD=123456789;";
    using (MySqlConnection con = new MySqlConnection(strConnString))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            cmd.CommandText = query;
            using (MySqlDataAdapter sda = new MySqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.