【发布时间】:2019-07-06 15:15:24
【问题描述】:
我已经成功地从 Excel 文件中的表中加载了数据表对象。如何在 UPDATE 查询中使用此数据表对象来更新现有 SQL Server 表?
我遇到这个错误:
调用的目标已抛出异常。
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj,对象 [] 参数,对象 [] 参数) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化) 在 System.RuntimeType.InvokeMember(字符串名称、BindingFlags bindingFlags、Binder binder、Object 目标、Object[] providedArgs、ParameterModifier[] 修饰符、CultureInfo 文化、String[] namedParams) 在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Public Sub Main()
Dim fileToTest As String
Dim SheetName As String
Dim connectionString As String
Dim excelConnection As OleDbConnection
Dim excelCommand As OleDbCommand
Dim ODA As OleDbDataAdapter
Dim dtExcel As New DataTable()
Dim SQLConn As SqlClient.SqlConnection
Dim SQLCmd As SqlClient.SqlCommand
Dim SQLPara As SqlClient.SqlParameter
'open a connection to the excel file
fileToTest = "C:\Users\testuser\Documents\test\mytestfile.xls"
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
fileToTest & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
excelConnection = New OleDbConnection(connectionString)
excelConnection.Open()
'open a SQL connection to the LRPSF_Source_DB SQL Server DB
connectionString = "Data Source=mysqlserver.net\sqlentdb1d;Trusted_Connection=True;DATABASE=LRPSF_Source_DB;CONNECTION RESET=FALSE"
SQLConn = New SqlClient.SqlConnection(connectionString)
SQLConn.Open()
'fetch the data from TEST table in Excel file using a command query and store in datatable object
SheetName = "TEST$"
excelCommand = excelConnection.CreateCommand()
excelCommand.CommandText = "SELECT * FROM [" & SheetName & "]"
excelCommand.CommandType = CommandType.Text
ODA = New OleDbDataAdapter(excelCommand)
ODA.Fill(dtExcel) '<- this datatable object is filled with the data successfully
'using the dtExcel datatable as a table input, update the existing dbo.TEST_INPUT_SIMPLE SQL Server table
SQLCmd = SQLConn.CreateCommand()
SQLCmd.CommandText = "UPDATE TIS SET TIS.MY_COLUMN = TISX.MY_COLUMN " &
"FROM dbo.TEST_INPUT_SIMPLE TIS INNER JOIN @source AS TISX " &
"ON TIS.UPDATE_ID = TISX.UPDATE_ID"
SQLCmd.CommandType = CommandType.Text
SQLCmd.Parameters.AddWithValue("@source", dtExcel).SqlDbType = SqlDbType.Structured
SQLCmd.ExecuteNonQuery() '<-- the program errors on this line
Dts.TaskResult = ScriptResults.Success
End Sub
【问题讨论】:
标签: sql-server excel vb.net ssis script-task