首页 文章

c#使用DataSet更新Access数据库表

提问于
浏览
0

我正在将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();
    }
}

1 回答

  • 0

    我在这里找到了我的问题的答案: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]);
        }
    }
    

相关问题