我在C#中有一个类似下面的方法,我要参数化我的查询,但是名字字段在数据库中获取空值.testTable(id,name)和id是auto increament.mysql ver是5.6
private void buttonTest_Click(object sender, EventArgs e)
{
string query = "insert into testdb.testTable(name) VALUES (@name) ;";
MySqlConnection conDataBase = new MySqlConnection(myfunctions.ConnString);
conDataBase.Open();
MySqlCommand cmd = new MySqlCommand(query, conDataBase);
try
{
cmd.Connection = conDataBase;
cmd.Parameters.AddWithValue("@name","John");
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
答案 0 :(得分:0)
尝试使用此替代varchar,50输入您的dbtype
string query = "insert into testdb.testTable(name) VALUES(_name) ;";
和
cmd.Connection = conDataBase;
cmd.Parameters.Add("_name", MySqlDbType.VarChar, 50).Value = "John";
cmd.ExecuteNonQuery();
答案 1 :(得分:0)
试试这个
command.Parameters.Add("@name", SqlDbType.VarChar,50);
command.Parameters["@name"].Value = "john";