【问题标题】:c# Update Access Database Table with DataSetc#用DataSet更新Access数据库表
【发布时间】:2014-11-24 15:35:25
【问题描述】:

我正在将一个 vba 程序转换为 c#。 vba 程序使用来自不同记录集 rst2 的值修改/添加行到记录集 rst1。 c# 版本使用 OleDbDataAdapter 将查询中的数据填充到数据集 csrst1 表 0 中。

在vba版本中,rst1.update好像是直接修改数据库表行。问题是,有没有办法在更新/修改数据集后更新访问数据库行?有没有更好的方法来做到这一点?

这是我到目前为止所做的......

vba:
rst1.FindFirst "[L1]=" & ![VendorID] & " AND [D1]=#" & ![REQ_IP_DATE] & "#"
If rst1.NoMatch Then
    rst1.AddNew
    '...new row fields set
    rst1.Update
Else
    rst1.Edit
    '...rows edited
    rst1.Update
End If

(上面的代码在 With rst2 do 循环中。rst1 字段使用来自 rst2 的值进行更新。)

c#:
foreach(DataRow dr in csrst2.Tables[0].Rows)
{
    var findfirst = csrst1.Tables[0].Select("[L1]=" + dr[0] + "  AND [D1]=#" + dr[1] + "#");
    if(findfirst.Count() < 1)
    {
        var newRow = csrst1.Tables[0].NewRow();
        //...new row fields set
        csrst1.Tables[0].Rows.Add(newRow);
    }
    else
    {   
        findfirst[0].BeginEdit();
        //...rows edited
        findfirst[0].AcceptChanges();
    }
}

【问题讨论】:

    标签: c# ms-access .net-4.0 vba


    【解决方案1】:

    我在这里找到了我的问题的答案:http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx,使用 adapter.update。这似乎真的很慢(执行 141 行需要 15-20 秒),所以如果有其他解决方案我想听听!

    这就是我所做的:

    c#:
    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
    foreach(DataRow dr in csrst2.Tables[0].Rows)
    {
        var findfirst = csrst1.Tables[0].Select("[L1]=" + dr[0] + "  AND [D1]=#" + dr[1] + "#");
        if(findfirst.Count() < 1)
        {
            var newRow = csrst1.Tables[0].NewRow();
            //...new row fields set
            csrst1.Tables[0].Rows.Add(newRow);
            adapter.Update(csrst1.Tables[0]);
        }
        else
        {   
            findfirst[0].BeginEdit();
            //...rows edited
            findfirst[0].AcceptChanges();
            adapter.Update(csrst1.Tables[0]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多