谁用过MySql的ADO.NET接口呀,碰一问题,希望帮忙。
这是我用Sql的ADO.NET接口写的。
string connection = "server=.;database=tempdb;uid =sa; pwd =sa;";
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into xx values(@a,@b)";
SqlParameter p1 = new SqlParameter();
p1.ParameterName = "@a";
p1.SqlDbType = SqlDbType.Int;
p1.SqlValue = "1";
SqlParameter p2 = new SqlParameter();
p2.ParameterName = "@b";
p2.SqlDbType = SqlDbType.VarChar;
p2.SqlValue = "bb";
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();
con.Close();
这样写没有问题,数据都能写入数据库的,但换成MYSQL的就不行了,写入数据库后都是null,不知道为什么,请高手指点。
改为MYSQL的写法。
string connection = "server=.;database=tempdb;uid =sa; pwd =sa;";//连接语句也换一下
MySqlConnection con = new MySqlConnection (connection);
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into xx values(@a,@b)";
MySqlParameter p1 = new MySqlParameter();
p1.ParameterName = "@a";
p1.MySqlDbType = MySqlDbType.Int;
p1.SqlValue = "1";
MySqlParameter p2 = new MySqlParameter();
p2.ParameterName = "@b";
p2.MySqlDbType = MySqlDbType.VarChar;
p2.SqlValue = "bb";
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();
con.Close();
------解决方案--------------------mysql 里@是不能用的,要用?
cmd.CommandText = "insert into xx values(?,?)";
MySqlParameter p1 = new MySqlParameter();
p1.MySqlDbType = MySqlDbType.Int;
p1.SqlValue = "1";
------解决方案--------------------cmd.CommandText = "insert into xx values(?a,?b)";