我有一些我继承的 SqlAdapter 代码。我不得不承认,代码通常运行得很好,但是最近有一种方法确实表现得很奇怪。当我附加 SQL Server Profiler 时,它显示调用了存储过程,但发送的所有参数都是 NULL。我快要发疯了,试图追查这件事。
我传递给 oSqlAdapter 对象的数据集包含我想要插入的所有正确数据。oSqlAdapter.InsertCommand 具有正确的存储过程名称和所有正确的参数。我已经对参数的顺序和类型以及参数和列名的拼写进行了三次检查。我还能错过什么?
代码:
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