从 SQLite 中删除主键行不起作用
Delete primary key row from SQLite doesn't work
正如我的标题所述,我在从 SQLite 数据库中删除时遇到了问题。
让我解释一下。
第一个是 Windows 表单应用程序。
第二个 .NET Framework 4.8
3d Nugget 包 SQLite Core
我的创建、插入、搜索数据库工作正常。我有一个搜索,它是一个文本框,当用户搜索某些内容并从 db 返回正确的信息时,它为他们提供了更新(尚未实现)或使用 2 个单独按钮删除(显然)的选项。现在,我的删除方法从搜索框中获取值并尝试删除当前行(注意搜索值是来自数据库的主键)。
当我按下“删除”按钮时,我的程序冻结了,但没有立即显示错误,而在一段时间后仍在调试过程中,它显示我的数据库已锁定。
CallStack 说它要睡觉了。该错误发生在 cmd.ExecuteNonQuery
行。
我已经在整个网络和此处搜索相关问题,但没有找到适合我的东西。我在针对初学者的 C# 论坛中有一个关于这个相同问题的公开 post。还没有找到解决方案,有些人试图帮助我。
我不太精通 SQLite。希望我提供了足够的信息。
这是我目前正在尝试的。
Delete.cs
class Delete_Record
{
string path = "Injection_Settings.db";
public static TextBox Search_Box = new TextBox();
public static TextBox mould_code_input = new TextBox();
public static TextBox machine_number_input = new TextBox();
public static TextBox machine_type_input = new TextBox();
public static TextBox supplier_input = new TextBox();
public static ComboBox colour_input = new ComboBox();
public static ComboBox comboBox1 = new ComboBox();
public static ComboBox comboBox2 = new ComboBox();
public static ComboBox comboBox3 = new ComboBox();
public static NumericUpDown numericUpDown1 = new NumericUpDown();
public static NumericUpDown numericUpDown2 = new NumericUpDown();
public static NumericUpDown numericUpDown3 = new NumericUpDown();
public static DateTimePicker dateTimePicker1 = new DateTimePicker();
public static TextBox item_name_input = new TextBox();
public static PictureBox pictureBox1 = new PictureBox();
public static PictureBox pictureBox2 = new PictureBox();
public void Delete_Info_From_DB()
{
int Mould_Code = Int32.Parse(Search_Box.Text);
try
{
using (SQLiteConnection con = new SQLiteConnection(@"Data Source = " + path))
{
con.Open();
using (SQLiteCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"DELETE FROM Description WHERE Mould_Code =@Mould_Code";
cmd.Prepare();
cmd.Parameters.AddWithValue("@Mould_Code", Mould_Code);
cmd.ExecuteNonQuery();
if ((cmd.CommandText = @"SELECT * FROM Description WHERE Mould_Code = " + Mould_Code + "") == null)
{
MessageBox.Show("Επιτυχής διαγραφή.");
mould_code_input.Text = null;
machine_number_input.Text = null;
machine_type_input.Text = null;
supplier_input.Text = null;
colour_input.Text = null;
comboBox1.Text = null;
comboBox2.Text = null;
comboBox3.Text = null;
numericUpDown1.Text = null;
numericUpDown2.Text = null;
numericUpDown3.Text = null;
dateTimePicker1.Text = null;
item_name_input.Text = null;
pictureBox1.Image = null;
pictureBox2.Image = null;
}
}
con.Close();
}
}
catch (Exception)
{
MessageBox.Show("Αποτυχία διαγραφής.");
}
}
}
删除 Form1 中的按钮
private void Delete_Record_Click(object sender, EventArgs e)
{
Delete_Record dr = new Delete_Record();
Delete_Record.Search_Box.Text = Search_Box.Text;
Delete_Record.mould_code_input = mould_code_input;
Delete_Record.machine_number_input = machine_number_input;
Delete_Record.machine_type_input = machine_type_input;
Delete_Record.supplier_input = supplier_input;
Delete_Record.colour_input = colour_input;
Delete_Record.comboBox1 = comboBox1;
Delete_Record.comboBox2 = comboBox2;
Delete_Record.comboBox3 = comboBox3;
Delete_Record.numericUpDown1 = numericUpDown1;
Delete_Record.numericUpDown2 = numericUpDown2;
Delete_Record.numericUpDown3 = numericUpDown3;
Delete_Record.dateTimePicker1 = dateTimePicker1;
Delete_Record.item_name_input = item_name_input;
Delete_Record.pictureBox1 = pictureBox1;
Delete_Record.pictureBox2 = pictureBox2;
if (Database_Search.Search_Box.Text != null)
{
dr.Delete_Info_From_DB();
}
}
我发现我的 problem.I 在我的搜索中忘记关闭 SqlDataReader methods.Thanks 大家帮忙。
正如我的标题所述,我在从 SQLite 数据库中删除时遇到了问题。
让我解释一下。
第一个是 Windows 表单应用程序。
第二个 .NET Framework 4.8
3d Nugget 包 SQLite Core
我的创建、插入、搜索数据库工作正常。我有一个搜索,它是一个文本框,当用户搜索某些内容并从 db 返回正确的信息时,它为他们提供了更新(尚未实现)或使用 2 个单独按钮删除(显然)的选项。现在,我的删除方法从搜索框中获取值并尝试删除当前行(注意搜索值是来自数据库的主键)。
当我按下“删除”按钮时,我的程序冻结了,但没有立即显示错误,而在一段时间后仍在调试过程中,它显示我的数据库已锁定。
CallStack 说它要睡觉了。该错误发生在 cmd.ExecuteNonQuery
行。
我已经在整个网络和此处搜索相关问题,但没有找到适合我的东西。我在针对初学者的 C# 论坛中有一个关于这个相同问题的公开 post。还没有找到解决方案,有些人试图帮助我。 我不太精通 SQLite。希望我提供了足够的信息。
这是我目前正在尝试的。 Delete.cs
class Delete_Record
{
string path = "Injection_Settings.db";
public static TextBox Search_Box = new TextBox();
public static TextBox mould_code_input = new TextBox();
public static TextBox machine_number_input = new TextBox();
public static TextBox machine_type_input = new TextBox();
public static TextBox supplier_input = new TextBox();
public static ComboBox colour_input = new ComboBox();
public static ComboBox comboBox1 = new ComboBox();
public static ComboBox comboBox2 = new ComboBox();
public static ComboBox comboBox3 = new ComboBox();
public static NumericUpDown numericUpDown1 = new NumericUpDown();
public static NumericUpDown numericUpDown2 = new NumericUpDown();
public static NumericUpDown numericUpDown3 = new NumericUpDown();
public static DateTimePicker dateTimePicker1 = new DateTimePicker();
public static TextBox item_name_input = new TextBox();
public static PictureBox pictureBox1 = new PictureBox();
public static PictureBox pictureBox2 = new PictureBox();
public void Delete_Info_From_DB()
{
int Mould_Code = Int32.Parse(Search_Box.Text);
try
{
using (SQLiteConnection con = new SQLiteConnection(@"Data Source = " + path))
{
con.Open();
using (SQLiteCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"DELETE FROM Description WHERE Mould_Code =@Mould_Code";
cmd.Prepare();
cmd.Parameters.AddWithValue("@Mould_Code", Mould_Code);
cmd.ExecuteNonQuery();
if ((cmd.CommandText = @"SELECT * FROM Description WHERE Mould_Code = " + Mould_Code + "") == null)
{
MessageBox.Show("Επιτυχής διαγραφή.");
mould_code_input.Text = null;
machine_number_input.Text = null;
machine_type_input.Text = null;
supplier_input.Text = null;
colour_input.Text = null;
comboBox1.Text = null;
comboBox2.Text = null;
comboBox3.Text = null;
numericUpDown1.Text = null;
numericUpDown2.Text = null;
numericUpDown3.Text = null;
dateTimePicker1.Text = null;
item_name_input.Text = null;
pictureBox1.Image = null;
pictureBox2.Image = null;
}
}
con.Close();
}
}
catch (Exception)
{
MessageBox.Show("Αποτυχία διαγραφής.");
}
}
}
删除 Form1 中的按钮
private void Delete_Record_Click(object sender, EventArgs e)
{
Delete_Record dr = new Delete_Record();
Delete_Record.Search_Box.Text = Search_Box.Text;
Delete_Record.mould_code_input = mould_code_input;
Delete_Record.machine_number_input = machine_number_input;
Delete_Record.machine_type_input = machine_type_input;
Delete_Record.supplier_input = supplier_input;
Delete_Record.colour_input = colour_input;
Delete_Record.comboBox1 = comboBox1;
Delete_Record.comboBox2 = comboBox2;
Delete_Record.comboBox3 = comboBox3;
Delete_Record.numericUpDown1 = numericUpDown1;
Delete_Record.numericUpDown2 = numericUpDown2;
Delete_Record.numericUpDown3 = numericUpDown3;
Delete_Record.dateTimePicker1 = dateTimePicker1;
Delete_Record.item_name_input = item_name_input;
Delete_Record.pictureBox1 = pictureBox1;
Delete_Record.pictureBox2 = pictureBox2;
if (Database_Search.Search_Box.Text != null)
{
dr.Delete_Info_From_DB();
}
}
我发现我的 problem.I 在我的搜索中忘记关闭 SqlDataReader methods.Thanks 大家帮忙。