【发布时间】:2020-12-25 22:54:45
【问题描述】:
我对 c# 比较陌生,我在我的大学从事一个项目,其中包括验证交换机,我现在正在处理 CRUD 以编辑、创建和删除 datagridview 中一列的项目,所以这是我的问题.
我有一个按钮可以作为我对 crud 的更新,它从文本框中获取文本并将文本框中的文本替换为一行中的文本,所以基本上当我点击我想要更改的行时然后输入新文本,当我单击按钮进行更新时,它会更新我的 datagridview 上的所有行,所以我的目标是只用文本框中的文本编辑一行。
private void button6_Click(object sender, EventArgs e)
{
string strcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Verificacao de Quadros
Eletricos\Verificação de Quadros Elétricos\bin\Debug\DatabaseENG.accdb";
string comando = @"UPDATE [Norms Table]
set [Drills (according to the norm EN 61439-2)]
= @drills";
using (OleDbConnection con = new OleDbConnection(strcon))
{
using (OleDbCommand com = new OleDbCommand(comando, con))
{
com.Parameters.Add("@drills", OleDbType.VarChar).Value = textBox5.Text;
try
{
con.Open();
com.ExecuteNonQuery();
MessageBox.Show("Drill succesfully edited");
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
}
}
所以这里是更新的代码,感谢您的时间。
【问题讨论】:
-
UPDATE查询需要一个WHERE子句来限制更新的内容。通常,这可能类似于WHERE id = 3,或者主/唯一键/值引用可能被命名的任何名称。没有它,每一行肯定会得到更新。 -
所以它会是
@"UPDATE FROM [Norms Table] where [Drills (according to the norm EN 61439-2)] = @drills";? -
不完全是:
UPDATE [Norms Table] SET ... WHERE ... -
string comando = @"UPDATE [Norms Table] SET [Drills (according to the norm EN 61439-2)] WHERE = @drills"; -
需要应用过滤器的字段。
UPDATE tablename SET fieldname = something WHERE ID=somerecordID
标签: c# ms-access datagridview oledb crud