将数据从 SQL Anywhere 移动到 SQL Server,经过几次故障后,一切进展顺利,直到我收到错误
无法修改列“A_Future”,因为它是计算列或 UNION 的结果’
公平地说,我正在从 SELECT * 插入数据——因此使用它仅返回未计算的列。
'Remove computed columns from the SELECT query
Dim DR() As DataRow = MSSQLDT.Select("Computed = 'N'")
Dim vSelectedRows As Integer = DR.Count
Dim vCurrentRow As Integer = 0
strSQL = "SELECT "
For Each Row In DR
strSQL += Row("Name")
vCurrentRow += 1
If vCurrentRow = vSelectedRows Then
strSQL += " "
Else
strSQL += ", "
End If
Next
strSQL += "FROM " & vTable
这将返回以下查询字符串...
SELECT Transaction_ID, Debit, Credit, Paid, P_Description, Document_Date, Supplier_ID, Nominal_Transaction, HOA_Code, Document_Saved, Document_ID, Document_No, Supplier_Inv_No, Type, Paid_Date, Part_Paid, Open_Editing, Editing_Name, Updated_Name, Updated, Reserve_Item, Hold, eCheck_Pending, eSig_Required, eSigOne_ID, eSigTwo_ID FROM A_Purchase_Ledger
.. 并且它不包含任何计算列。再次运行,但它仍然抛出相同的错误(对于所有计算列)
Microsoft.Data.SqlClient.SqlException (0x80131904): The column "A_Future" cannot be modified because it is either a computed column or is the result of a UNION operator.
The column "A_Current" cannot be modified because it is either a computed column or is the result of a UNION operator.
The column "A_30" cannot be modified because it is either a computed column or is the result of a UNION operator.
The column "A_60" cannot be modified because it is either a computed column or is the result of a UNION operator.
The column "A_90" cannot be modified because it is either a computed column or is the result of a UNION operator.
The column "A_Older" cannot be modified because it is either a computed column or is the result of a UNION operator.
现在这没有任何意义,因为我没有尝试向其中插入数据,除非存在SqlBulkCopy
我不知道的怪癖?
选择数据被添加到DataTable
然后它进入这个函数
Public Function BulkUpdate_DataMS(DT As DataTable, HOAID As Integer, IsHASoftware As Boolean, TableName As String) As Boolean
Try
Using Conn As New Microsoft.Data.SqlClient.SqlConnection
If IsHASoftware = True Then
Conn.ConnectionString = HASConString
Else
Conn.ConnectionString = ReturnConnStringMS(HOAID)
End If
Conn.Open()
Using vTrans = Conn.BeginTransaction
Using vBulk As New Microsoft.Data.SqlClient.SqlBulkCopy(Conn, Microsoft.Data.SqlClient.SqlBulkCopyOptions.Default, vTrans)
vBulk.DestinationTableName = TableName
vBulk.WriteToServer(DT)
End Using
vTrans.Commit()
End Using
Conn.Close()
End Using
Return True
Catch ex As Exception
EmailError(ex, 222, PageName)
Return False
End Try
End Function
您需要设置,
ColumnMappings
否则它只会进行序数匹配。此外,无需事务,因为批量插入将使用其自己的事务。如果您有事务,则
Using
无需手动关闭连接。您可能还应该考虑使用
Async
和Await
来完成所有这些工作。执行异步的正确方法不是使用,而是直接在标记为的函数中批量插入时
Task.Run
使用Await
Async
DataTable
此外,如果您正在从另一台服务器读取数据,则根本不需要将其保存在 a 中。您可以将从/DbDataReader
获取的数据直接传递给/ 。ExecuteReader
ExecuteReaderAsync
WriteToServer
WriteToServerAsync
这不是我想要的解决方案,需要遍历数千行,但批量更新似乎在涉及计算值的地方不起作用。这会检查是否有任何计算值,然后一次插入一个。如果有人有更好的解决方案,我很乐意听听。
基本上,动态地拉动每个
Table
模式,将SQL Anywhere
其更改为SQL Server
,添加表,SQL Server
然后插入数据