我知道这是一个简单的问题,但我找不到错误。我想将数据保存在我的数据库中。我用Microsoft SQL Server 2008创建了一个名为"Examen“的数据库,然后在visual studio的应用程序中创建了如下连接字符串:
string connectionstring = @"Data Source=.\sqlexpress;Initial Catalog=Examen;Integrated Security=True";然后我使用下面的代码将数据插入到"Test“表中:
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();
}
}运行此代码时,我在打开连接时出错
Unable to connect to any of the specified MySQL hosts.发布于 2014-07-07 20:45:43
是否确实要连接到MySQL服务器?如果要连接到http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx,请使用MSSQL。
此外,您还应该熟悉SQL injection
https://stackoverflow.com/questions/24610856
复制相似问题