【发布时间】: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();
}
}
【问题讨论】: