我有一些我繼承的SqlAdapter代碼。我不得不承認,代碼通常運行得非常好,但最近有一種方法真的很奇怪。當我連接SQL Server Profiler時,它顯示存儲過程被調用,但所有發送的參數都是NULL。我正在努力追查這件事。VB SqlAdapter批量更新:InsertCommand向存儲過程發送「NULL」
傳遞給oSqlAdapter對象的數據集具有我想要插入的所有正確數據。 oSqlAdapter.InsertCommand具有正確的存儲過程名稱和所有正確的參數。我tripple檢查參數的順序和類型以及參數和列名稱的拼寫。我可以錯過什麼?
代碼:
Dim rows as DataRow() = myDataSet.Tables(0).Select(Nothing, Nothing, _
DataViewRowState.Added)
... Create oSqlAdapter and connect the currentConnectionObject ...
With oSqlAdapter
.InsertCommand = New SqlCommand()
.InsertCommand.CommandText = "myStoredProc"
.InsertCommand.CommandType = CommandType.StoredProcedure
.InsertCommand.Connection = currentConnectionObject
AddParameter(.InsertCommand, "@Parm1", ParameterDirection.Input, "MyColumn1")
AddParameter(.InsertCommand, "@Parm2", ParameterDirection.Input, "MyColumn2")
AddParameter(.InsertCommand, "@Parm3", ParameterDirection.Input, "MyColumn3")
AddParameter(.InsertCommand, "@ResultCode", ParameterDirection.Output, "MyResult")
.Update(rows) '<--- Crashes here. When I connect Profiler, I see that
' the update did send the parameters, but the values
' are always NULL, never the values in the DataRows.
End With
oSqlAdapter = Nothing
的AddParameter邏輯如下:
Public Sub AddParameter(cmd As SqlCommand, parmName As String, _
dir as ParameterDirectoni, colName as String)
Dim oParm As SqlParameter = New SqlParameter(parmName, SqlDbType.Int)
With oParm
.Direction = dir
.Value() = Nothing
.SourceColumn = colName
End With
cmd.Parameters.Add(oParm)
oParm = Nothing
End Sub
你檢查過行嗎?也許這是空的?你有沒有添加行到表? – 2013-02-18 20:58:30
「我傳遞給oSqlAdapter對象的數據集具有我想要插入的所有正確數據。」是。 – Jerry 2013-02-18 21:04:03
他們都是整數?也許你有一個類型問題,因爲proc將所有4個參數的類型指定爲一個整數。 – darin 2013-02-18 21:23:12