C# 删除sql中的行?
在这个AddPatient.cs中,我无法删除整行,但是当通过delete按钮操作在C中执行此操作时,它不起作用,因此我需要帮助C# 删除sql中的行?,c#,sql,sql-delete,C#,Sql,Sql Delete,在这个AddPatient.cs中,我无法删除整行,但是当通过delete按钮操作在C中执行此操作时,它不起作用,因此我需要帮助 private void Delete_Click(object sender, EventArgs e) { if (pno.Text != "" || pname.Text != "" || age.Text != "" || sex.Text != "" || add.Text != "" || city.Text != "" |
private void Delete_Click(object sender, EventArgs e)
{
if (pno.Text != "" || pname.Text != "" || age.Text != "" || sex.Text != "" || add.Text != "" || city.Text != "" || phone.Text != "" || edate.Text != ""
|| dname.Text != "" || dig.Text != "" || dpname.Text != "")
try
{
cn.Open();
cmd.CommandText =
"delete from PatientDetails where PatientNumber= '" + pno.Text + "' and PatientName='" + pname.Text + "' and Age='" + age.Text + "' and Sex ='" + sex.Text + "' and PatientAddress='" + add.Text + "' and City='" + city.Text + "' and PhoneNumber='" + phone.Text + "' and EntryDate='" + edate.Text + "' and DoctorName='" + dname.Text + "' and Diagnosis='" + dig.Text + "' and DepartmentName='" + dpname.Text + "'";
cmd.ExecuteNonQuery();
cmd.Clone();
cn.Close();
MessageBox.Show("Record Deleted Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
where子句中的分隔符为and或or,not:
但是,您确实应该按id删除,而不是按长列列表删除
也可以考虑使用参数化查询。将用户输入直接包含到字符串中不仅会造成SQL注入危险,还会破坏系统。例如,如果其中一个名字是奥康纳。
听说过主键吗?听说过实体框架或nhibernate吗不要用那种手工的东西 请定义不工作。这是一个错误还是该行未被删除?定义1您正在使用的实际数据库,2不工作的真正含义-您是否收到错误?如果是,有什么错误??它只是不起任何作用吗?请:使用参数化查询,不要像这样连接SQL!!`cmd.Clone;`???并且该sql不会执行。阅读一些如何编写sql的教程,当我单击“删除”时,它不起作用@阿德萨南西瓦。编辑问题并在变量替换后打印查询。错误可能是显而易见的。
"delete from PatientDetails where PatientNumber= '" + pno.Text + "' and PatientName='" + pname.Text + "' and Age='" + age.Text + "' and Sex ='" + sex.Text + "' and PatientAddress='" + add.Text + "' and City='" + city.Text + "' and PhoneNumber='" + phone.Text + "' and EntryDate='" + edate.Text + "' and DoctorName='" + dname.Text + "' and Diagnosis='" + dig.Text + "' and DepartmentName='" + dpname.Text + "'";