Question

I'm connecting my windows form application to SQL server but I get this error "SqlException was unhandled" when I start the program.

Here is my code:

void ShowEmployees()
    {
        using (SqlConnection Connect = new SqlConnection("Data Source=(local);" + "Database='SanMarDryCleaners';" + "Integrated Security=SSPI;"))
        {
            string strEmployees = "SELECT * FROM employees;";

            SqlCommand cmdEmployees = new SqlCommand(strEmployees, Connect);

            SqlDataAdapter daEmployees = new SqlDataAdapter();

            daEmployees.SelectCommand = cmdEmployees;
            DataSet dsEmployees = new DataSet("EmployeesSet");
            daEmployees.Fill(dsEmployees);

            Connect.Open();

            dgvEmployees.DataSource = dsEmployees;
            dgvEmployees.DataMember = dsEmployees.Tables[0].TableName;
        }
    }

Here is the screenshot: enter image description here

Was it helpful?

Solution

The error suggest that it can't find the SQL Server. And that makes sense because you are using a SqlConnection class which is for Microsoft SQL Server only. You have to use MySQL libary.

Download it from this site:

http://dev.mysql.com/downloads/connector/net/

OTHER TIPS

The error points to a problem while connecting to the server.

A SqlConnection is for SQL-Servers. Use a library specific to MySQL and a MySqlConnection.

See:

Connector/Net Installation

MySQL Connector/Net connection strings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top