【问题标题】:Stored procedure error in vb.netvb.net中的存储过程错误
【发布时间】:2013-12-03 09:56:31
【问题描述】:

这是我的功能:

     Public Function getLocation(ByVal res As String, ByVal tblName As String) As DataTable
    Dim command As SqlCommand = New SqlCommand("jk_getLocation", cn)
    command.Parameters.AddWithValue("@id", res)
    command.Parameters.AddWithValue("@TableName", tblName)

    If resDT.Rows.Count <> 0 Then
        resDT.Rows.Clear()
    End If
    resDS.Clear()
    Dim da As New SqlDataAdapter(command)
    Try
        da.Fill(resDS, "nb")
        resDT = resDS.Tables(0)
    Catch exe As SqlException
        logFile("SP getLocation ----" + exe.Message)
    Catch ex As Exception
        logFile("SP getLocation ----" + ex.Message)
    End Try
    Return resDT
End Function

我就是这么称呼它的:

resDT = nb.getLocation(res.ToString(), code)

这是我的存储过程:

ALTER procedure [dbo].[jk_getLocation]
@id varchar(100),
@TableName varchar(100)
as
begin
select * from b where id=@id and name=@TableName
end

当我从 SQL 执行我的程序时,它工作正常并返回正确的数据.. 但是当我从 vb.net 运行它时,我不断收到Procedure or function 'jk_getLocation' expects parameter '@id', which was not supplied.,尽管我正在传递所有参数

【问题讨论】:

  • var char(100) 应该是 varchar(100) 吗?假设复制和粘贴错误。
  • 好吧,它是 varchar(100)...
  • 您在 Db 中的 ID 是否是整数?此外,您是否正在逐步确保在程序开始之前,您的 @id 参数正在被填充

标签: sql sql-server vb.net stored-procedures vb.net-2010


【解决方案1】:

您忘记将命令类型设置为CommandType.StoredProcedure

也就是说,您可能想知道为什么会出现错误

过程或函数“jk_getLocation”需要参数“@id”,但未提供该参数

如果您运行 SQL Profiler 并按照当前的方式执行您的方法,您将看到类似于以下行的内容

exec sp_executesql N'jk_getLocation',
            N'@id nvarchar(3),@TableName nvarchar(6)',
              @id=N'100',@TableName=N'foobar'

这是当您使用带参数的 CommandText SqlCommand(最常与参数化查询一起使用)时 SQL 服务器执行的操作

如果你在 SSMS 中执行上述操作,你会得到同样的错误

您可以通过添加来解决此问题

command.CommandType = CommandType.StoredProcedure

或通过更改命令文本以包含参数名称

Dim command As SqlCommand = New SqlCommand("jk_getLocation @id, @tablename", cn)

将被执行为

exec sp_executesql N'jk_getLocation @id, @tablename', //Note the parameters declared here
            N'@id nvarchar(3),@TableName nvarchar(6)',
              @id=N'100',@TableName=N'foobar'

【讨论】:

    【解决方案2】:

    试试这个:

    Public Sub getLocation(resDT As DataTable, params As Hashtable)
        Dim command As SqlCommand = New SqlCommand
    
        command.Connection = cn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "jk_getLocation"
        command.CommandTimeout = 10
    
        For i = 0 To ht.Count - 1
            command.Parameters.AddWithValue(params.Keys(i), params.Values(i))
        Next
    
        Dim adapter as New SqlDataAdapter(command)
    
        Try
            adapter.fill(resDT)
        Catch ex As Exception
            logFile("SP getLocation ----" + ex.Message)
        End Try
    End Sub
    

    调用方法:

    Dim params As New Hashtable
    
    params.Add("@id", res)
    params.Add("@TableName", tblName)
    nb.getLocation(resDT, params)
    

    【讨论】:

      【解决方案3】:
      Public Function getLocation(ByVal res As String, ByVal tblName As String) As DataTable
          Dim command As SqlCommand = New SqlCommand()
          cn.Open()
          command.Connection = cn
          command.CommandType = CommandType.StoredProcedure
          command.CommandText = "jk_getLocation"
          command.Parameters.Add("@id", SqlDbType.VarChar).Value = res
          command.Parameters.Add("@TableName", SqlDbType.VarChar).Value = tblName
      
          Dim sda As SqlDataAdapter
          sda = New SqlDataAdapter(command)
          Dim dt As DataTable
          dt = New DataTable
      
          sda.Fill(dt)
      
          Return dt
      
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-11
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 2013-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多