存储过程错误'无效列'

时间:2012-11-18 07:25:01

标签: .net sql

我想将以下C#代码从普通SQL更改为存储过程,但它始终在列为Invalid Column的列上显示错误。存储过程有什么问题?

CREATE PROCEDURE [dbo].[usp_AdminRegPage1]
     (@VendorId VARCHAR)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT WorksPackage,MajorWorks FROM dbo.RegPage1 WHERE Vendor_Id = @VendorId;
END

protected void DisplayRecord()
{
     SqlConnection SqlCon = new SqlConnection(GetConnectionString());
     string sql = "SELECT WorksPackage,MajorWorks from RegPage1 
                                                 WHERE Vendor_ID='"+lblPage1ID.Text+"'";
     var da = new SqlDataAdapter(sql, SqlCon);
     var ds = new DataSet();

     da.Fill(ds);

     if (ds.Tables[0].Rows.Count > 0)
     {
        txtMajorPackages.Text = ds.Tables[0].Rows[0]["WorksPackage"].ToString();
        txtRegP1Works.Text = ds.Tables[0].Rows[0]["MajorWorks"].ToString();
     }
}

1 个答案:

答案 0 :(得分:1)

像这样的事情应该做的工作

public void DisplayRecord()
{
    SqlDataReader rdr = null;

    // create and open a connection object
    SqlConnection SqlCon = new SqlConnection(GetConnectionString());

    try
    {

        SqlCon.Open();

        // 1. create a command object identifying
        // the stored procedure
        SqlCommand cmd = new SqlCommand(
            "usp_AdminRegPage1", SqlCon);

        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;

        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(
            new SqlParameter("@VendorId", lblPage1ID.Text));

        // execute the command
        rdr = cmd.ExecuteReader();

        // iterate through results, printing each to console
        while (rdr.Read())
        {
            txtMajorPackages.Text = rdr["WorksPackage"];
            txtRegP1Works.Text = rdr["MajorWorks"];
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {
        SqlCon.Close();
    }
}

取自本网站的示例
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx