[英]SQL DELETE statement in C#
I'm just asking for help.. it doesn't delete the row. 我只是在寻求帮助..它不会删除该行。 .but i didn't encountered any error.. please help me..my code is:
。但是我没有遇到任何错误..请帮助我..我的代码是:
private void btndelete_Click(object sender, EventArgs e)
{
if (txtprn.Text == "" || txtdescription.Text == "")
{
MessageBox.Show("No selected file to be delete!",
"Delete Data",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning);
}
else
{
DialogResult answer;
answer = MessageBox.Show("Are you sure you want to delete this record?",
"Delete Record",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (answer == DialogResult.Yes)
{
con.Open();
com.CommandText =
@"DELETE
FROM tblsupply
WHERE (prnumber = @prnumber AND
description = @description)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@prnumber", txtprn.Text);
com.Parameters.AddWithValue("@description", txtdescription.Text);
com.ExecuteNonQuery();
MessageBox.Show("Record Deleted!");
con.Close();
}
ClearFields();
GridRefresh();
}
}
Check the value of output. 检查输出值。 If it returns a positive number, that many records are deleted successfully.
如果返回正数,则表示已成功删除许多记录。 If you get a -1 a rollback occurs, the return value is also -1.
如果得到-1,则会发生回滚,返回值也为-1。
Convert the txtprn to int as it looks like a number and you are passing as string. 将txtprn转换为int,因为它看起来像一个数字,并且您正在作为字符串传递。
com.Parameters.AddWithValue("@prnumber", Convert.ToInt32(txtprn.Text));
com.Parameters.AddWithValue("@description", txtdescription.Text);
var numberOfRowsAffected = com.ExecuteNonQuery();
MessageBox.Show("Record Deleted!"+numberOfRowsAffected );
I was suggesting not to delete a record based on user input. 我建议不要基于用户输入删除记录。 In your case, you should have an identifier that is not editable by user even more hidden value that your application is managing behind the scene.
在您的情况下,您应该拥有一个用户不可编辑的标识符,甚至是您的应用程序在后台管理的更多隐藏值。 Scenario:
场景:
In your current state, note that the deletion may not succeed in case that are spaces for example so you cannot rely on those texts for deletion. 在当前状态下,请注意,例如在空格的情况下,删除可能不会成功,因此您不能依靠这些文本进行删除。
Place this try catch so you'll know the error 放置此try catch,以便您会发现错误
try
{
DialogResult answer;
answer = MessageBox.Show("Are you sure you want to delete this record?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (answer == DialogResult.Yes)
{
con.Open();
com.CommandText = "DELETE FROM tblsupply WHERE (prnumber=@prnumber AND description=@description)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@prnumber", txtprn.Text);
com.Parameters.AddWithValue("@description", txtdescription.Text);
com.ExecuteNonQuery();
MessageBox.Show("Record Deleted!");
con.Close();
}
ClearFields();
GridRefresh();
}
catch(Exception ex)
{
ex.Message.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.