使用 DataGrid 更新访问表时出错

Posted

技术标签:

【中文标题】使用 DataGrid 更新访问表时出错【英文标题】:Error on updating access table using DataGrid 【发布时间】:2013-01-25 07:05:57 【问题描述】:

我正在使用 DataGridView 来更新、插入或删除表中的数据。该代码可以很好地插入数据,但是在更新或删除数据或行时会出错。代码如下。

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) OR ( Tray_No = ?)) AND (('.

如何消除这个错误?

【问题讨论】:

最后一个“AND”运算符后是否缺少某些内容? 【参考方案1】:

试试这个。

 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);
       string sql="write your Delete query"
      OleDbCommand dCommand = new OleDbCommand(sql, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(sAdapter);
       sAdapter.DeleteCommand=dCommand
        sAdapter.Update(sTable);
    


//Save...
private void button4_Click(object sender, EventArgs e)

     string sql="write your update query"
     OleDbCommand uCommand = new OleDbCommand(sql, connection);
     OleDbCommandBuilder builder = new OleDbCommandBuilder(sAdapter);
     sAdapter.UpdateCommand=uCommand 
    sAdapter.Update(sTable);
    dataGridView1.ReadOnly = true;
    button4.Enabled = false;
    button2.Enabled = true;
    button3.Enabled = true;

【讨论】:

看这篇文章,这是添加参数的好方法。***.com/questions/1966275/…【参考方案2】:

你有一些非常奇怪的字段名称,前导下划线是什么??

但框架似乎无法自动生成插入和更新 SQL。

查看以下SO 问题,该问题描述了将您自己的 UPDATE 和 INSERT 语句添加到适配器。

或者,您可以考虑使用 VisualStudio 的 DataSet 设计器。

【讨论】:

以上是关于使用 DataGrid 更新访问表时出错的主要内容,如果未能解决你的问题,请参考以下文章

在存储过程中使用日期时间作为参数更新表时出错

尝试使用 pyspark 访问 greenplum 表时出错

更新我的 sql 中的两个表时出错

更新表时 H2 数据库“重命名文件时出错”

Azure Synapse 分析:访问外部表时出错

H2 - 使用 SQL 语句“SELECT * FROM null T”访问链接表时出错