使用c#中的sqlParameters插入sqlserver
void BtnConfirmClick(object sender, EventArgs e)
{
string first = txtFname.Text;
string last = txtLname.Text;
string query = "INSERT INTO customer (first, last)";
query += " VALUES (@fstName,@lstName)";
string url = "Server=localhost;Database=flight;uid=root;password=1234";
con1 = new MySqlConnection(url);
con1.Open();
cmd1 = new MySqlCommand(query,con1);
//SqlParameter myParam = new SqlParameter("@fstName", SqlDbType.VarChar, 55);
//myParam.Value = last;
//SqlParameter myParam2 = new SqlParameter("@lstName", SqlDbType.VarChar, 55);
//myParam2.Value = first;
cmd1.Parameters.AddWithValue("@fstName", first);
cmd1.Parameters.AddWithValue("@lstName", last);
//cmd1.Parameters.Add(myParam);
//cmd1.Parameters.Add(myParam2);
cmd1.ExecuteNonQuery();
con1.Close();
}
我无法将数据插入到sqlServer,它不断出现错误。 请帮助!
void BtnConfirmClick(object sender, EventArgs e)
{
string first = txtFname.Text;
string last = txtLname.Text;
string query = "INSERT INTO customer (first, last)";
query += " VALUES (@fstName,@lstName)";
string url = "Server=localhost;Database=flight;uid=root;password=1234";
con1 = new MySqlConnection(url);
con1.Open();
cmd1 = new MySqlCommand(query,con1);
//SqlParameter myParam = new SqlParameter("@fstName", SqlDbType.VarChar, 55);
//myParam.Value = last;
//SqlParameter myParam2 = new SqlParameter("@lstName", SqlDbType.VarChar, 55);
//myParam2.Value = first;
cmd1.Parameters.AddWithValue("@fstName", first);
cmd1.Parameters.AddWithValue("@lstName", last);
//cmd1.Parameters.Add(myParam);
//cmd1.Parameters.Add(myParam2);
cmd1.ExecuteNonQuery();
con1.Close();
}
I can't insert data into sqlServer, it keeps getting an error.
Please Help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
乍一看,您忘记了结束 char (; 在您的 sql 查询中)。您需要:
Ar the first glance you have forgot about ending char (; in your sql query). You need: