简体   繁体   中英

Unable to connect to any of the specified MySQL hosts

I know that it is an easy problem but I can't find the error. I want to save data in my database. I create a database named "Examen" with Microsoft SQL Server 2008 then in my app in visual studio I make the connection string like this :

string connectionstring = @"Data Source=.\sqlexpress;Initial Catalog=Examen;Integrated Security=True";

Then I use this code to insert data into a "Test" table:

MySqlConnection connection = new MySqlConnection(connectionstring);
MySqlCommand cmd;
connection.Open();

try
{
  cmd = connection.CreateCommand();
  cmd.CommandText = "Insert into Examen.Test (nom,prenom) values (" + txbnom.Text + "," + txbprenom.Text + ") ";
  cmd.ExecuteNonQuery();
  MessageBox.Show("ok");
}
catch (Exception)
{
  throw;
}
finally
{ 
  if(connection.State == ConnectionState.Open)
  {
    connection.Close();
  }
}

When running this code i had an error when openning the connection

Unable to connect to any of the specified MySQL hosts.

You are mixing MySQL and MSSQL .

Are you sure you want to connect to a MySQL server? Use http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx if you would like to connect to MSSQL.

Also you should make yourself familiar with SQL injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM