0

我在下面有以下 ASP.NET (VB) 代码。我的目标是获取接受 1 个参数的存储过程的输出并将该信息存储到数据表中:

        Dim strConnection1 As String = System.Configuration.ConfigurationManager.AppSettings("Rdf2012")
        Dim conn1 As System.Data.SqlClient.SqlConnection
        conn1 = New System.Data.SqlClient.SqlConnection(strConnection1)
        Dim cmd1 As New SqlCommand
        Dim prm1 As New SqlParameter
        Dim dtDataTable1 As DataTable
        cmd1.Connection = conn1
        cmd1.CommandText = "usp_GetDetails"
        cmd1.CommandType = CommandType.StoredProcedure
        prm1 = cmd1.Parameters.Add(strProgramID, SqlDbType.VarChar, 50)
        prm1.Direction = ParameterDirection.Input

        conn1.Open()

        Dim dataAdapter As New SqlDataAdapter
        dataAdapter.SelectCommand = cmd1

        Dim ds1 As New DataSet
        dataAdapter.Fill(ds1)
        conn1.Close()

        dtDataTable1 = ds1.Tables("Table1")

程序停在

    dataAdapter.Fill(ds1) and says:

@1012952-4403 不是过程 usp_GetDetails 的参数

这是存储过程的样子:

    ALTER PROCEDURE [dbo].[usp_GetDetails]
@ProId VARCHAR(20) = NULL
    AS
4

3 回答 3

1

你需要做

prm1 = cmd1.Parameters.AddWithValue ("@ProID", strProgramID)

代替

 prm1 = cmd1.Parameters.Add(strProgramID, SqlDbType.VarChar, 50) 
 prm1.Direction = ParameterDirection.Input 

除非另有说明,否则将假定输入方向。

于 2012-08-02T20:34:13.057 回答
1

验证 strProgramID 等于什么。您可能在需要参数名称的地方传递参数值。如果是这种情况,请更换

Dim prm1 As New SqlParameter
....
prm1 = cmd1.Parameters.Add(strProgramID, SqlDbType.VarChar, 50)
prm1.Direction = ParameterDirection.Input

 cmd1.Parameters.AddWithValue("@ProId",strProgramID)
于 2012-08-02T20:38:27.397 回答
1

您以错误的方式创建参数,请尝试以下操作:

Dim strConnection1 As String = System.Configuration.ConfigurationManager.AppSettings("Rdf2012")
    Dim conn1 As System.Data.SqlClient.SqlConnection
    conn1 = New System.Data.SqlClient.SqlConnection(strConnection1)
    Dim cmd1 As New SqlCommand
    Dim dtDataTable1 As DataTable
    cmd1.Connection = conn1
    cmd1.CommandText = "usp_GetDetails"
    cmd1.CommandType = CommandType.StoredProcedure
    cmd1.Parameters.Add("@ProId",strProgramID)

    conn1.Open()

    Dim dataAdapter As New SqlDataAdapter
    dataAdapter.SelectCommand = cmd1

    Dim ds1 As New DataSet
    dataAdapter.Fill(ds1)
    conn1.Close()

    dtDataTable1 = ds1.Tables("Table1")
于 2012-08-02T20:40:01.453 回答