当我删除记录时,它可以正常工作。但是当我关闭程序并再次打开它时,实际上记录不会被删除。
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();
}
当我搜索时,某些记录无法删除,因为它们是“主”记录。如何删除记录TRULY?
答案 0 :(得分:3)
你不需要引用' @ 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?