如何在C#Windows应用程序P-SQL(MS Access oledb连接C#)中更新Listview子项
我正试图编写一个程序,其中包括c#windows窗体应用程序中基于数据库(Ms access)的数据采集系统 申请书包括: *文本框 *列表视图 *钮扣 *ms access oledb数据库连接 除了用作按钮的更新功能外,一切都正常 在这种情况下(更新功能按钮),首先,我想用新数据填充文本框,如ıd number等…,然后选择我要更改的行,然后单击更新按钮,用输入的数据更改整行,这些数据在文本框中可用。但我无法更改 基本上,ı希望使用文本框中可用的新数据更改listview(数据库)的行 我在这里添加了删除功能代码,它允许我删除listview和数据库中的选定行。它工作正常。我希望以与删除功能按钮相同的方式创建此更新功能按钮 删除功能按钮(工作正常,删除数据库和Listview中的选定行):如何在C#Windows应用程序P-SQL(MS Access oledb连接C#)中更新Listview子项,c#,sql,listview,ms-access,C#,Sql,Listview,Ms Access,我正试图编写一个程序,其中包括c#windows窗体应用程序中基于数据库(Ms access)的数据采集系统 申请书包括: *文本框 *列表视图 *钮扣 *ms access oledb数据库连接 除了用作按钮的更新功能外,一切都正常 在这种情况下(更新功能按钮),首先,我想用新数据填充文本框,如ıd number等…,然后选择我要更改的行,然后单击更新按钮,用输入的数据更改整行,这些数据在文本框中可用。但我无法更改 基本上,ı希望使用文本框中可用的新数据更改listview(数据库)的行 我
private void button1_Click(object sender, EventArgs e)
{
connection.Open();//open database
//delete command
OleDbCommand cm = new OleDbCommand("DELETE FROM Table1 WHERE IDNumber=@IDNumber", connection);
cm.Parameters.AddWithValue("@IDNumber", listView1.SelectedItems[0].Text);
cm.ExecuteNonQuery();//Girmiş olduğumuz parametreyi geri gönderir
connection.Close();
listView1.Items.Clear();
showdatas();
}
更新按钮代码,以与删除功能按钮相同的方式运行(应使用文本框中的新数据更新所选行):
private void button1_Click(object sender, EventArgs e)
{
connection.Open();//open database
//delete command
OleDbCommand cm = new OleDbCommand("DELETE FROM Table1 WHERE IDNumber=@IDNumber", connection);
cm.Parameters.AddWithValue("@IDNumber", listView1.SelectedItems[0].Text);
cm.ExecuteNonQuery();//Girmiş olduğumuz parametreyi geri gönderir
connection.Close();
listView1.Items.Clear();
showdatas();
}
该代码与上述代码不同:
private void button3_Click(object sender, EventArgs e)
{
//you can change selected things in the list(update button)
connection.Open();
OleDbCommand ab = new OleDbCommand("insert into Table1 WHERE IDNumber=@IDNumber (ProductNumber,ProductDeveloperName,ProductDeveloperSurname,ProductDeveloperID,ProductDeveloperAge,ProductCost) values ('" + textBox1.Text.ToString() + "','" + textBox6.Text.ToString() + "','" + textBox7.Text.ToString() + "','" + textBox3.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + textBox5.Text.ToString() + "' )", connection);
ab.Parameters.AddWithValue("@IDNumber", listView1.SelectedItems[0].Text);
ab.ExecuteNonQuery();
connection.Close();
listView1.Items.Clear();
showdatas();
}
您的查询语法错误。您应该将列名放在表名旁边
insert into Table1(ProductNumber,ProductDeveloperName,ProductDeveloperSurname,ProductDeveloperID,ProductDeveloperAge,ProductCost)...
不能将WHERE
与INSERT
语句一起使用。它甚至不存在于世界上
还有几件事
- 你应该经常使用。这种类型的字符串连接对攻击是开放的
- 用于自动处理连接和命令,而不是手动调用
方法Close
- 不要使用
。使用AddWithValue
方法重载指定参数类型及其大小Add
ProductNumber
,ProductDeveloperID
,ProductDeveloperAge
,ProductCost
应该是数字类型的列,而不是字符。检查列类型两次。没有。1)我无法理解如何在代码中实现此使用功能。2) 在学习参数化查询之前,我想让这个更新函数工作3)我更改了Sql代码,如:“插入到表1(ProductNumber、ProductDeveloperName、ProductDeveloperSurname、ProductDeveloperID、ProductDeveloperAge、ProductCost)值(“+textBox1.Text.ToString()+”、“+textBox6.Text.ToString()+”、“”“+textBox7.Text.ToString()+”、“+textBox3.Text.ToString()+”、“+textBox2.Text.ToString()+”、“+textBox5.Text.ToString()+”),连接);5) 它们都是字符串类型,我不打算做任何额外的数学计算。6) 你能帮我让这个更新按钮工作吗?7) 我将非常感激