SQL删除命令
本文关键字:命令 删除 SQL | 更新日期: 2023-09-27 18:17:06
当我删除一条记录时,它工作了。但是当我关闭程序并再次打开它时,实际上记录并没有被删除。
private void button5_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.'SQLEXPRESS;AttachDbFilename=D:'Upper_Stage_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
con.Open();
MessageBox.Show("Delete Current Row of Database Open");
if (MaxRows != 0)
{
SqlCommand Command = new SqlCommand("DELETE FROM Table1 WHERE Data_no='@inc'", con);
try
{
Command.ExecuteNonQuery();
MessageBox.Show("Record Deleted");
}
catch (SqlException)
{
MessageBox.Show("ERROR: Could not delete record");
}
MaxRows--;
if (inc != 0)
{
inc--;
NavigationRecords();
}
else
{
if (MaxRows != 1)
{
inc++;
NavigationRecords();
}
else
{
MessageBox.Show("No Record");
}
}
}
else
{
MessageBox.Show("No Record");
}
con.Close();
MessageBox.Show("Delete Current Row of Database Closed");
con.Dispose();
}
当我搜索时,有些记录不能被删除,因为它们是"主"记录。我怎样才能真正删除记录?
你不需要在'@inc':
SqlCommand Command = new SqlCommand("DELETE FROM Table1 WHERE Data_no=@inc", con);
您还需要提供@inc
参数的值。
Command.Parameters.AddWithValue("@inc", inc); //inc is a variable that contain a value for the @inc parameter
虽然直接指定类型并使用Value
属性比AddWithValue
更好。查看更多详细信息:Can we stop using AddWithValue() already?