通过dataGridView更新表时出错... [英] Error on updating table through dataGridView...
问题描述
我正在尝试使用DataGridView对表执行插入,更新和删除操作。
参考: http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm [<一个href =http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htmtarget =_ blanktitle =New Window> ^ ]
我的数据库是Microsoft Access 2007.它插入良好但更新和删除时出错。该守则如下......
I am trying to perform insert, update, delete actions on table using DataGridView.
Reference: http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm[^]
My database is Microsoft Access 2007. It inserts well but on updating and deleting gives an error. The Code is as following...
OleDbCommand sCommand;
OleDbDataAdapter sAdapter;
OleDbCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
//Load Data...
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + Application.StartupPath + @"\Label-Data.accdb";
string sql = "SELECT * FROM _Default";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
sCommand = new OleDbCommand(sql, connection);
sAdapter = new OleDbDataAdapter(sCommand);
sBuilder = new OleDbCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "_Default");
sTable = sDs.Tables["_Default"];
connection.Close();
dataGridView1.DataSource = sDs.Tables["_Default"];
dataGridView1.ReadOnly = true;
button4.Enabled = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
//Add or Edit...
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.ReadOnly = false;
button4.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
}
//Delete...
private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
sAdapter.Update(sTable);
}
}
//Save...
private void button4_Click(object sender, EventArgs e)
{
sAdapter.Update(sTable);
dataGridView1.ReadOnly = true;
button4.Enabled = false;
button2.Enabled = true;
button3.Enabled = true;
}
它在sAdapter.Update(sTable)上出错;线。错误是查询表达式中的语法错误((ID =?)AND((?= 1 AND Prn IS NULL)OR(Prn =?))AND((?= 1 AND _Copy IS NULL)OR(_Copy = ?))AND((?= 1 AND _Item IS NULL)OR(_Item =?))AND((?= 1 AND _Design IS NULL)OR(_Design =?))AND((?= 1 AND Tray_No IS NULL)或(Tray_No =?))AND((''。任何建议?
It gives an error on sAdapter.Update(sTable); line. The error is "Syntax error in query expression ''((ID = ?) AND ((? = 1 AND Prn IS NULL) OR (Prn = ?)) AND ((? = 1 AND _Copy IS NULL) OR (_Copy = ?)) AND ((? = 1 AND _Item IS NULL) OR (_Item = ?)) AND ((? = 1 AND _Design IS NULL) OR (_Design = ?)) AND ((? = 1 AND Tray_No IS NULL) OR (Tray_No = ?)) AND ((''." Any suggestions?
推荐答案
您的查询不正确。检查
这篇关于通过dataGridView更新表时出错...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!