如何在 vb.net & ms 访问中使用 DataSet 和 DataAdapter 一次插入多条记录?
How to insert multiple records at once using DataSet and DataAdapter in vb.net & ms access?
我试图使用数据集将记录从一个 table 插入到另一个,首先是代码:
Try
Dim ds1 As New DataSet
Dim dbada2 As New OleDbDataAdapter
Dim dbbl2 As New OleDbCommandBuilder
Dim dbcon As OleDbConnection = New OleDbConnection(dbconstring)
dbcon.Open()
Dim cmd1 As OleDbCommand = New OleDbCommand("SELECT InspectionID, ICTDIR, UnitDescription, SizeType, QuantityNo, SerialNo FROM DeliveryInspection WHERE ICTDIR = @ictdir", dbcon)
cmd1.Parameters.AddWithValue("@ictdir", txtRefNo.Text)
Dim dbada1 As New OleDbDataAdapter(cmd1)
ds1.Clear()
dbada1.Fill(ds1)
dbcon.Close()
dbcon.Open()
dbada2 = New OleDbDataAdapter("SELECT * FROM LogSUF", dbcon)
dbada2.Update(ds1)
dbcon.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, "DBADA 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
这里我有 2 个数据适配器,第一个来自 table DeliveryInspection
,第二个来自 LogSUF
。两个 table 都有相同的列,唯一的区别是 LogSUF
没有记录。
我想制作一个系统,将 DeliveryInspection
中的记录(与用户键入它的文本框中相应的 ICTDIR
txtRefNo
)插入到LogSUF
。我的代码没有错误,但它也不起作用,我尝试了一些方法来解决这个问题。请帮忙,还有另一种方法可以做到这一点吗?我是初学者。
当您调用 Update
时,只有 RowState
为 Added
、Modified
或 Deleted
的行将被保存。就目前而言,您所有的行都有 RowState
的 Unchanged
,因此没有什么可保存的。
解决方案是在调用 Fill
之前,在第一个数据适配器上将 AcceptChangesDuringFill
设置为 False
。这样,当您调用 Fill
时,不会隐式调用 AcceptChanges
方法,并且每个 RowState
将保持 Added
,准备好在您调用 [=10] 时插入该行=].
我试图使用数据集将记录从一个 table 插入到另一个,首先是代码:
Try
Dim ds1 As New DataSet
Dim dbada2 As New OleDbDataAdapter
Dim dbbl2 As New OleDbCommandBuilder
Dim dbcon As OleDbConnection = New OleDbConnection(dbconstring)
dbcon.Open()
Dim cmd1 As OleDbCommand = New OleDbCommand("SELECT InspectionID, ICTDIR, UnitDescription, SizeType, QuantityNo, SerialNo FROM DeliveryInspection WHERE ICTDIR = @ictdir", dbcon)
cmd1.Parameters.AddWithValue("@ictdir", txtRefNo.Text)
Dim dbada1 As New OleDbDataAdapter(cmd1)
ds1.Clear()
dbada1.Fill(ds1)
dbcon.Close()
dbcon.Open()
dbada2 = New OleDbDataAdapter("SELECT * FROM LogSUF", dbcon)
dbada2.Update(ds1)
dbcon.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, "DBADA 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
这里我有 2 个数据适配器,第一个来自 table DeliveryInspection
,第二个来自 LogSUF
。两个 table 都有相同的列,唯一的区别是 LogSUF
没有记录。
我想制作一个系统,将 DeliveryInspection
中的记录(与用户键入它的文本框中相应的 ICTDIR
txtRefNo
)插入到LogSUF
。我的代码没有错误,但它也不起作用,我尝试了一些方法来解决这个问题。请帮忙,还有另一种方法可以做到这一点吗?我是初学者。
当您调用 Update
时,只有 RowState
为 Added
、Modified
或 Deleted
的行将被保存。就目前而言,您所有的行都有 RowState
的 Unchanged
,因此没有什么可保存的。
解决方案是在调用 Fill
之前,在第一个数据适配器上将 AcceptChangesDuringFill
设置为 False
。这样,当您调用 Fill
时,不会隐式调用 AcceptChanges
方法,并且每个 RowState
将保持 Added
,准备好在您调用 [=10] 时插入该行=].