継承した SqlAdapter コードがいくつかあります。認めざるを得ないのは、コードは一般的に非常にうまく動作するということですが、最近、本当に奇妙な動作をしているメソッドが 1 つあります。SQL Server プロファイラーをアタッチすると、ストアド プロシージャが呼び出されたことが示されますが、送信されるパラメーターはすべて 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