必须声明标量变量@colname

时间:2013-12-06 11:20:56

标签: sql exception parameters dataadapter

我有一个dataadapter,我是通过IDE创建的。我已经给它一个查询,类似于以下(我通过删除额外的标准和列简化了它):

SELECT     *
FROM         MyView
WHERE    (Colname like '%' + @colname + '%')

生成的代码如下所示:

Public Overloads Overridable Function FillBySearchCriteria(ByVal dataTable As MaqisDataSet.VFleetDataTable, ByVal __colname As String) As Integer
            Me.Adapter.SelectCommand = Me.CommandCollection(1)
            If (__rego Is Nothing) Then
                Throw New Global.System.ArgumentNullException("__colname")
            Else
                Me.Adapter.SelectCommand.Parameters(0).Value = CType(__colname, String)
            End If
            If (Me.ClearBeforeFill = true) Then
                dataTable.Clear
            End If
            Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
            Return returnValue
       End Function

当我执行此操作时(无论是通过__colname传递值,还是将其保留为空,无论哪种方式)我都遇到了这个异常:

  

OleDbException(0x80040e14):必须声明标量变量“@colname”。

我不知道是什么导致这种情况,我添加了一些调试,似乎有一个有效的参数,在SelectCommand.Parameters集合中名为“@colname”。

这是追踪:

[OleDbException (0x80040e14): Must declare the scalar variable "@colname".]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +1003520
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +255
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +188
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +161
   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +116
   System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +4
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +130
   System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +162
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +115

任何寻找原因的帮助都将不胜感激。感谢

0 个答案:

没有答案