删除记录成功,但它会删除Windows窗体中数据库中的所有记录 [英] Delete a record successful, but it delete all records in the database in windows forms

查看:114
本文介绍了删除记录成功,但它会删除Windows窗体中数据库中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是运行程序时datagridview(照片)的链接:



[这是链接 ]



我想当用户选择ID 9并单击删除按钮时,它会删除所选行并在数据库中删除它。



我怎么能这样做?我的意思是,我应该把命令放在什么地方?我知道我的命令缺少WHERE子句,但我不知道在WHERE子句中放什么。



这是我的代码现在一直在做(下面的代码是一键删除所有行)





这是代码:



使用(OleDbConnection conn = new OleDbConnection(connectionString))

{

string query =DELETE FROM [Record] ;

conn.Open();



使用(OleDbCommand cmd = new OleDbCommand(query,conn))

{

使用(OleDbDataAdapter adapter = new OleDbDataAdapter(query,conn))

{

DataTable ds = new DataTable();

adapter.Update(ds);

dataGridView.DataSource = ds;

cmd.ExecuteNonQuery();

}

}

}



现在,我的问题是,当用户选择单行时,如何从datagridview中删除它并将其更新到数据库?请帮忙。谢谢。



i试过这段代码:



deleteButton.Click + = new System.EventHandler(this .DeleteRecord);



私人DeleteRecord(对象发件人,EventArgs e)

{

使用(OleDbConnection conn) = new OleDbConnection(connectionString))

{

string query =DELETE FROM [Record] WHERE [ID] = 9;

conn .Open();



使用(OleDbCommand cmd = new OleDbCommand(query,conn))

{

使用(OleDbDataAdapter adapter = new OleDbDataAdapter(query,conn))

{

DataTable ds = new DataTable();

adapter.Update (ds);

dataGridView.DataSource = ds;

cmd.ExecuteNonQuery() ;

}

}

}

}



上面的代码工作正常,但是选择ID为9的行,如果我得到ID 10,ID 11等等,直到ID 100,我不想逐个查询。有没有办法做到这一点?我的意思是,当用户选择一行时,无论它是什么ID,都会删除该行并将其更新到数据库。谢谢

Here is the link of datagridview (photo) when i run the program:

[Here is the link]

I want when the user select "ID 9" and click the "delete" button, it will delete the selected row also delete it at database.

How could i do that? I mean, what should i put in my command? I know my command is missing the "WHERE" clause, but i don't know what to put in the "WHERE" clause.

Here is the code that i have been doing right now (the following code is delete all row in a single click)


Here is the code:

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record]";
conn.Open();

using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}

Now, my problem is, how do i delete it from datagridview and update it to the database when the user select a single row? Please help. Thanks.

i have tried this code:

deleteButton.Click += new System.EventHandler(this.DeleteRecord);

private DeleteRecord(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record] WHERE [ID] = 9";
conn.Open();

using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}
}

The above code is working, but it is select the row where the ID is 9, what if i got ID 10, ID 11 and so on until ID 100, i didn't want to put the query one by one. Is there any ways to do that? I mean, when the user select a row, no matter what ID it is, it will be delete the row and update it to the database.. Thanks

推荐答案

是,你应该在你的查询中提到WHERE标准。

Yes, you should mention WHERE criteria in your query.
string strID = "";//Assign the selected value ... 9
string query = "DELETE FROM [Record] WHERE ID='" + strID + "'";



并等待 OriginalGriff 关于SQL注入的答案。


And wait for OriginalGriff's answer regarding SQL injection.


您需要使用以下内容,

字符串查询=(从表格中删除ID = 9,conn)



在这里,您可以将ID从9更改为任何内容。我想你知道如何在其中使用变量。别问。
You need to use the following,
String query = ("delete from tablename where ID=9,conn)

In here, You can change the ID from 9 to anything. I think You know how to use a variable in it. Else ask.


这篇关于删除记录成功,但它会删除Windows窗体中数据库中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆