使用复合键删除 [英] Delete using composite key
本文介绍了使用复合键删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,该表具有付款目的和付款日期作为组合键.我正在尝试
使用我的表单从表中删除一行.代码正在运行,但没有从表中删除行
表单后面的代码
I have a table which has purpose and dateofpayment as it composite key. I am trying
to use my form to delete a row from the table. The code is running but it is not deleting the row from the table
code behind the form
private void btnDelete5_Click(object sender, EventArgs e)
{
clsStaffsalarypayment person5 = new clsStaffsalarypayment();
if (MessageBox.Show("Are you really sure of the deletion", "Deletion", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
else
{
person5.ID5 = this.cboStaffid5.Text;
//person.savehistory();a
person5.removeRegister5();
}
}
类中的代码
code in class
public void removeRegister5()
{
//remove the data in memory
SqlCommand cmd = new SqlCommand();
string dat = null;
dat = dateofpayment.Year + "-" + dateofpayment.Month + "-" + dateofpayment.Day;
string sqlQuery = null;
sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = '" + purpose + "' and dateofpayment ='" + dat + "'";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
}
推荐答案
在生成查询字符串的行(即sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = ''" + purpose + "'' and dateofpayment =''" + dat + "''";
.)上放置一个断点. 一旦程序停止执行,请将此查询复制到sql Server Management Studio中并运行它(替换参数后).
检查结果-它最有可能帮助您找到解决方案.
Put a breakpoint on the line that builds the query string i.e.sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = ''" + purpose + "'' and dateofpayment =''" + dat + "''";
.
Once the program stops execution, copy this query into sql server management studio and run it (after replacing the parameters).
Check the result - it should most likely help you get to the solution.
您好
它不会删除,因为条件返回false.检查目的是否正确,然后必须检查数据.如果所有这些都正确,请尝试以下操作:
Hello
It doesn''t delete because the condition returns false. Check the purpose if it''s correct then you must check the dat. If all of these are correct then try this:
sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = N'" + purpose + "' and dateofpayment ='" + dat + "'";
有人建议:
1.不要在查询后使用conn.Close()
,因为如果conn.Close
之前存在异常,则连接永远不会关闭.
在finally
块中使用它.或使用using
2.代码不需要else
:
Some advises:
1. Do not use conn.Close()
after a query because if there be an exception before than conn.Close
then the connection never be closed.
Use it in a finally
block. or use using
2. The code dosn''t need else
:
if (MessageBox.Show("Are you really sure of the deletion", "Deletion", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
//else
//{
person5.ID5 = this.cboStaffid5.Text;
//person.savehistory();a
person5.removeRegister5();
//}
这篇关于使用复合键删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文