VB.net使用存储过程传递参数

时间:2016-01-27 19:52:00

标签: sql vb.net function

我正在尝试调用填充数据表的函数,但它不起作用而且我迷路了。我收到一个功能错误。函数不会在所有代码路径上返回值。这是我的代码,请帮助谢谢。

Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
        strPartNumber = txtPartNumber.Text

            Dim CT As DataTable = Me.GetCompanyInfo(strPartNumber, "None")
            dvCommon.DataSource = CT
            dvCommon.DataBind()
    End Sub

Private Function GetCompanyInfo(ByRef PartNumber As String, ByRef Version As String) As DataTable
        Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("spCommonInfo")
                cmd.Connection = con
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@PartNumber", PartNumber)
                cmd.Parameters.AddWithValue("@Version", Version)
                Using sda As New SqlDataAdapter(cmd)
                    Dim CT As New DataTable()
                    sda.Fill(CT)
                End Using
            End Using
        End Using
    End Function

1 个答案:

答案 0 :(得分:0)

据我所知,您的方法GetCompanyInfo会将数据表作为As DataTable返回,但在代码中没有返回的地方。

您应该返回已填写的DataTable

Return CT

您的功能代码应为

Private Function GetCompanyInfo(ByRef PartNumber As String, ByRef Version As String) As DataTable
        Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
        Using con As New SqlConnection(constr)
        Dim CT As New DataTable()
            Using cmd As New SqlCommand("spCommonInfo")
                cmd.Connection = con
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@PartNumber", PartNumber)
                cmd.Parameters.AddWithValue("@Version", Version)
                Using sda As New SqlDataAdapter(cmd)                    
                    sda.Fill(CT)
                End Using
            End Using
        End Using
     Return CT
    End Function