初学MYSQL,若要使用MySQL自带的连接器进行连接,先下载(http://dev.mysql.com/downloads/connector/net/5.1.html)
引用DLL,下面是简单的测试代码。
using MySql.Data.MySqlClient;
....
string _strConnect = "server=localhost; user id=snow; password=snow; database=db_test";
MySqlConnection myConn=new MySqlConnection(_strConnect);
myConn.Open();
//注意在Oracle,mssql里参数前面都是带@,MySql要带?,不晓得为什么MySQL要搞特殊化,
string _sQuery = "insert into myfirst(Id,Name) values(?Id,?Name)";
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = myConn;
myCommand.CommandText = _sQuery;
MySqlParameter para1=new MySqlParameter("?Id",DbType.Int32); //这里要注意必须要用?号
MySqlParameter para2=new MySqlParameter("?Name",DbType.String);//这里要注意必须要用?号
para1.Value=5;
para2.Value="ddd";
cmd.Parameters.Add(para1);
cmd.Parameters.Add(para2);
cmd.ExecuteNonQuery();