当数据库中存在的序列号输入到文本框中时,会使用消息框成功删除它。问题是,即使文本框为空,它仍然表示它是成功的。
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string deleteEntry = "delete from Inventory where SerialNumber='" + txtSerial.Text + "' ";
MessageBox.Show(deleteEntry);
command.CommandText = deleteEntry;
//TO READ DATA
command.ExecuteNonQuery();
MessageBox.Show("Data Has Been Deleted");
connection.Close(); // CLOSE HERE OR YOU CANNOT EDIT RECORDS SIMULTANEOUSLY
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
connection.Close();
}
}
答案 0 :(得分:4)
如果您希望在txtSerial.Text
为空或为空时执行命令,请检查它:
if (!string.IsNullOrEmpty(txtSerial.Text)) {
try
{
connection.Open();
(...)
} else {
MessageBox.Show("Error: Provide a Serial")
}
此外,请查看parameterized queries
,并考虑在finally
块中关闭您的连接,或使用using
语句进行连接。
答案 1 :(得分:0)
if (txtSerial.Text.Length != 0 )
{
try
{
connection.Open();
}
Catch() .......
}
else
{
MessageBox.Show("Serial Number must be required...");
}
试试这个......