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

c#使用数据集更新Access数据库表,c#,ms-access,.net-4.0,vba,C#,Ms Access,.net 4.0,Vba,我正在将vba程序转换为c#。vba程序使用来自不同记录集rst2的值修改/添加记录集rst1中的行。c版本使用OleDbDataAdapter将查询中的数据填充到数据集csrst1表0中 在vba版本中,rst1.update似乎直接修改数据库表行。问题是,在更新/修改数据集之后,是否有方法更新access数据库行?有更好的方法吗 这是我到目前为止所做的 vba: rst1.FindFirst "[L1]=" & ![VendorID] & " AND [D1]=#" &

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

在vba版本中,rst1.update似乎直接修改数据库表行。问题是,在更新/修改数据集之后,是否有方法更新access数据库行?有更好的方法吗

这是我到目前为止所做的

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(csrst2.Tables[0]中的DataRow dr.行)
{
var findfirst=csrst1.表[0]。选择(“[L1]=”+dr[0]+”和[D1]=#“+dr[1]+”);
if(findfirst.Count()<1)
{
var newRow=csrst1.Tables[0].newRow();
//…已设置新行字段
csrst1.Tables[0].Rows.Add(newRow);
}
其他的
{   
findfirst[0]。BeginEdit();
//…已编辑的行
findfirst[0]。AcceptChanges();
}
}

我在这里找到了问题的答案:,使用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]);
    }
}
c#
OleDbCommandBuilder=新的OleDbCommandBuilder(适配器);
foreach(csrst2.Tables[0]中的DataRow dr.行)
{
var findfirst=csrst1.表[0]。选择(“[L1]=”+dr[0]+”和[D1]=#“+dr[1]+”);
if(findfirst.Count()<1)
{
var newRow=csrst1.Tables[0].newRow();
//…已设置新行字段
csrst1.Tables[0].Rows.Add(newRow);
adapter.Update(csrst1.Tables[0]);
}
其他的
{   
findfirst[0]。BeginEdit();
//…已编辑的行
findfirst[0]。AcceptChanges();
adapter.Update(csrst1.Tables[0]);
}
}
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]);
    }
}