为什么每次执行程序时都会出现不同类型的错误?
我正在尝试从MySql中获取数据并将其传输到MsAccess。 提取时,我的程序还使用从MySql提取的数据插入MsAccess。 这些是错误: '命令执行期间遇到致命错误' '在复制内存时检测到可能的I / O竞争状况。默认情况下,I / O包不是线程安全的。在多线程应用程序中,必须以线程安全的方式访问流,例如TextReader或TextWriter的Synchronized方法返回的线程安全包装器。这也适用于StreamWriter和StreamReader之类的类。'
Dim query As String
Dim query2 As String
Dim cmd As New OleDbCommand
Dim dt As New DataTable
For i As Integer = 0 To Dgvstoreinvoice.Rows.Count - 1
query = "SELECT * FROM hct_info_cap_order_sub_lotnumber WHERE hct_id_invoice = '" & Dgvstoreinvoice.Rows(i).Cells(0).Value & "'"
Dim data As New MySqlDataAdapter(query, conn)
data.Fill(dt) '- The errors occurs here
data.Dispose()
If dt.Rows.Count > 0 Then
query2 = "INSERT INTO [Ship-out Details]([InvoiceID],[Ship-outID],[Lot No],[Qty],[No of tray],[Packing No],[Carton no],[Type]) " & _
"VALUES(@InvoiceID,@ShipdusoutID,@Lot_No,@Qty,@No_of_tray,@Packing_No,@Carton_no,@type)"
For o As Integer = 0 To dt.Rows.Count - 1
updatelabel(lblLoadingMsg1, "[Save Data] of " & dt.Rows(o)("hct_lotnumber"))
cmd = New OleDbCommand(query2, oledb_conn2)
cmd.Parameters.AddWithValue("@InvoiceID", dt.Rows(o)("hct_id_invoice"))
cmd.Parameters.AddWithValue("@ShipdusoutID", dt.Rows(o)("hct_cap_order_sub_id"))
cmd.Parameters.AddWithValue("@Lot_No", dt.Rows(o)("hct_lotnumber"))
cmd.Parameters.AddWithValue("@Qty", dt.Rows(o)("hct_qty"))
cmd.Parameters.AddWithValue("@No_of_tray", dt.Rows(o)("hct_noofTray"))
cmd.Parameters.AddWithValue("@Packing_No", dt.Rows(o)("hct_packingNO"))
cmd.Parameters.AddWithValue("@Carton_no", dt.Rows(o)("hct_cartonNO"))
cmd.Parameters.AddWithValue("@type", dt.Rows(o)("hct_type"))
oleopen_(oledb_conn2)
cmd.ExecuteNonQuery()
oleclose_(oledb_conn2)
'saveShip_out_details(dt.Rows(o)("hct_id_invoice"), dt.Rows(o)("hct_cap_order_sub_id"), dt.Rows(o)("hct_lotnumber"), dt.Rows(o)("hct_qty"), _
' dt.Rows(o)("hct_noofTray"), dt.Rows(o)("hct_packingNO"), dt.Rows(o)("hct_cartonNO"), dt.Rows(o)("hct_type"))
Dim rows As New DataGridViewRow
rows.Cells.Add(New DataGridViewTextBoxCell With {.Value = dt.Rows(o)("hct_cap_order_sub_id")})
delegate_row_add(Dgvforpodetails, rows)
Next
End If
Next
答案 0 :(得分:2)
如建议的那样,您显然使这种方式变得比所需的难度更大。将数据从一个数据库复制到另一个数据库实际上只是在一个数据适配器上的Fill
调用,而在另一个数据库上的Update
调用。如果您的列名匹配,则可以使用命令构建器,但如果不匹配,则可以自己编写INSERT
语句。这是几个例子。
不匹配的列:
Dim sourceAdapter As New SqlDataAdapter("SELECT Column1, Column2 FROM SomeTable", "source connection string here")
Dim table As New DataTable
sourceAdapter.Fill(table)
Dim destinationConnection As New OleDbConnection("destination connection string here")
Dim destinationCommand As New OleDbCommand("INSERT INTO SomeOtherTable (ColumnA, ColumnB) VALUES (@ColumnA, @ColumnB)", destinationConnection)
With destinationCommand.Parameters
.Add("@ColumnA", OleDbType.VarChar, 50, "Column1")
.Add("@ColumnB", OleDbType.VarChar, 50, "Column2")
End With
Dim destinationAdapter As New OleDbDataAdapter With {.InsertCommand = destinationCommand}
destinationAdapter.Update(table)
匹配的列:
Dim sourceAdapter As New SqlDataAdapter("SELECT Column1 AS ColumnA, Column2 AS ColumnB FROM SomeTable", "source connection string here")
Dim table As New DataTable
sourceAdapter.Fill(table)
Dim destinationAdapter As New OleDbDataAdapter("SELECT ColumnA, ColumnB FROM SomeOtherTable", "destination connection string here")
Dim destinationBuilder As New OleDbCommandBuilder(destinationAdapter)
destinationAdapter.Update(table)