我是C#编程的新手。面对问题'First_Name'附近的语法不正确。在下面给出的代码中:
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=HP\SQLEXPRESS100;Database=CD_Gallery;Integrated Security=true";
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand("update Customer_Info First_Name ='" + fname.Text + "'");
//'" + fname.Text.ToString() + "','" + lname.Text.ToString() + "','" + landmark.Text.ToString() + "','" + address.Text.ToString() + "','" + contact.Text.ToString() + "','" + email.Text.ToString() + "','" + dateTimePicker1.Text.ToString() + "','" + deposite.Text.ToString() + "')", con);
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("You Have Successfully Updated");
Custid.Text = "";
fname.Text = "";
lname.Text = "";
address.Text = "";
contact.Text = "";
email.Text = "";
landmark.Text = "";
deposite.Text = "";
}
}
}
答案 0 :(得分:4)
问题:您忘记在更新语句中的表名后添加单词SET
。
解决方案1:在更新查询中添加表名后的SET
字样(不要推荐)
"update Customer_Info SET First_Name ='" + fname.Text + "'"
警告:您的查询对 sql注入攻击开放。请使用参数化查询来避免它们
解决方案2:使用参数化查询
替换它:
SqlCommand cmd = new SqlCommand("update Customer_Info SET First_Name
='"+fname.Text+"'");
有了这个:
SqlCommand cmd = new SqlCommand("update Customer_Info First_Name = @fname");
cmd.Parameters.AddWithValue("@fname" , fname.Text);
答案 1 :(得分:1)
您的问题不在C#中,在SQL语法中(您错过了set
关键字)
SqlCommand("update Customer_Info set First_Name ='" + fname.Text + "'");
答案 2 :(得分:0)
你缺少SET关键字:
update Customer_Info SET First_Name ='" + fname.Text + "'"
并提供where
子句,否则会更新表格中的所有记录。
答案 3 :(得分:0)
您在查询中缺少set关键字,您必须像这样设置
SqlCommand cmd = new SqlCommand("update Customer_Info set First_Name ='" + fname.Text + "'");