删除ms access数据库中的记录
本文关键字:记录 数据库 ms access 删除 | 更新日期: 2023-09-27 18:04:36
我已经使用这个代码从MS Access数据库表中删除记录。程序运行成功,但是数据库中的记录没有被删除。
private void button3_Click_2(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
String query=" delete from [student table] where Name="+txtname.Text+"";
command.CommandText= query;
MessageBox .Show ("Data deleted");
connection.Close();
}
下面的代码应该可以工作:
using (var myConnection = new OleDbConnection(myConnectionString))
using (var myCommand = myConnection.CreateCommand())
{
var nameParam = new OleDbParameter("@name", txtname.Text);
myCommand.CommandText = "DELETE FROM [student table] WHERE (Name) = @name";
myCommand.Parameters.Add(nameParam);
myConnection.Open();
myCommand.ExecuteNonQuery();
}