gpt4 book ai didi

c# - "Data type mismatch in criteria expression"尝试从数据库中删除行时

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:49 25 4
gpt4 key购买 nike

    OleDbCommand system = new OleDbCommand();
system.CommandType = CommandType.Text;
system.CommandText = "DELETE FROM Student WHERE(ID= '" +
txtStudentIDnumber.Text + "')";
system.Connection = mydatabase;

mydatabase.Open();
system.ExecuteNonQuery();
dataGridView1.Update();

this.tableAdapterManager.UpdateAll(csharrpfinalprojectDataSet);
mydatabase.Close();

MessageBox.Show("Student Record Deleted.", "deleting record...");

最佳答案

在您的命令文本中,您需要删除 txtStudentIDnumber.Text 周围的单引号 ('),因为它显示 ID 是整数类型,并且您将其作为字符串传递.以下应该修复错误。

system.CommandText = "DELETE FROM Student WHERE(ID= " + txtStudentIDnumber.Text + ")";

编辑:关于@mdb 评论,您应该始终使用 Parameters在您的查询中,这样您就可以避免 SQL Injection .请考虑以下事项:

OleDbCommand system = new OleDbCommand();
system.CommandType = CommandType.Text;
system.CommandText = "DELETE FROM Student WHERE ID = ?";
OleDbParameter parameter = new OleDbParameter("ID", txtStudentIDnumber.Text);
system.Parameters.Add(parameter);
system.Connection = mydatabase;

mydatabase.Open();
system.ExecuteNonQuery();
dataGridView1.Update();

关于c# - "Data type mismatch in criteria expression"尝试从数据库中删除行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com