asp.net存储过程输入/输出参数

时间:2015-06-29 21:46:27

标签: c# asp.net sql-server stored-procedures

我正在尝试将单个输入参数传递给存储过程,我认为该存储过程已声明OUTPUT参数,但似乎是输入/输出变量,因此给出了一条错误消息,指出其中一个参数不是提供了一个价值。

C#调用代码设置如下:

protected void CheckBoxClassRegion_btnSubmit(object sender, EventArgs e)
{
    date    @date;             /* Variable for the date of the game     */
    varchar @HomeTeam;         /* The name of the high school team      */
    varchar @AwayTeam;         /* The name of the other H.s. team       */
    int     @TeamID;           /* The ID number of the high school team */
    AddressText.Text = "";
    /**********************************************************************/
    /* The code below will initialize the connection to the database.     */
    /* As the connection string to the SQL database is defined as conn,   */
    /* the open method from conn will connect to the database, and the    */
    /* cmd variable will call on the stored procedure GetSchedule.        */
    /**********************************************************************/
    string strcon   = WebConfigurationManager.ConnectionStrings["FollowingHSFootballConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(strcon);
    conn.Open();
    SqlCommand cmd  = new SqlCommand("GetSchedule", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    /**********************************************************************/
    /* The for loop below will determine which items from the checkbox    */
    /* were selected from the input and use the High School team name to  */
    /* pass to the stored procedure 'GetSchedule' to return the dates,    */
    /* home team and away team each game.                                 */
    /**********************************************************************/
                                                                            /**********************************************************************/
    foreach (ListItem item in CheckBoxClassRegion.Items)                    /* This loop will go through all of the checkboxed items              */
    {                                                                       /**********************************************************************/
        if (item.Selected == true)                                          /*    If this team has been selected                                  */
        {                                                                   /**********************************************************************/
            cmd.Parameters.AddWithValue("@TeamName", item.Text);            /*       Pass input parameter "Team Name"                             */
            SqlDataReader reader = cmd.ExecuteReader();                     /*       Utilize the reader function to ensure all games are included */ 
            while (reader.Read())                                           /*       While there are still items to be read                       */
            {                                                               /**********************************************************************/
                cmd.Parameters.Add("@date", SqlDbType.Date);
                cmd.Parameters["@date"].Direction = ParameterDirection.Output;

                cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
                cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;

                cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
                cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();                                          /*       Execute the stored procedure                                 */

                Console.WriteLine(cmd.Parameters["@GameDate"].Value);
                Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
                Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
                Console.ReadLine(); 
            }

存储过程:

ALTER PROCEDURE [dbo].[GetSchedule]

    @teamname varchar(25),
    @date Date OUTPUT,
    @HomeTeam varchar(25) OUTPUT,
    @AwayTeam varchar(25) OUTPUT

AS
BEGIN

    SET NOCOUNT ON;


    SELECT HomeSchedule.Date, HomeTeam.HighSchoolName, AwayTeam.HighSchoolName
    from (
      (Schedule$ as HomeSchedule inner join HighSchoolFootballTeam$ as HomeTeam 
        on HomeSchedule.HomeTeamID = HomeTeam.HighSchoolTeamID)
     inner join 
      (Schedule$ as AwaySchedule inner join HighSchoolFootballTeam$ as AwayTeam
        on AwaySchedule.AwayTeamID = AwayTeam.HighSchoolTeamID) 
     on HomeSchedule.GameID = AwaySchedule.GameID)
     where HomeTeam.HighSchoolName = @teamname or AwayTeam.HighSchoolName = @teamname
    Order by HomeSchedule.Date

END

如何将@Date@HomeTeam@AwayTeam的输出变量充当输出而不是输入/输出变量,以便存储过程不会期望输入值来自他们?

1 个答案:

答案 0 :(得分:4)

您的存储过程具有@teamname参数,该参数被设置为输入参数,但您没有在C#代码中为@teamname参数设置任何值,因此这就是您收到错误的原因。您需要将值设置为@teamname参数,如下所示。

while (reader.Read())                                           /*       While there are still items to be read                       */
{                                                               /**********************************************************************/
    cmd.Parameters.Add("@teamname", SqlDbType.VarChar, 25);
    cmd.Parameters["@teamname"].Value = ...; // set the value here

    cmd.Parameters.Add("@date", SqlDbType.Date);
    cmd.Parameters["@date"].Direction = ParameterDirection.Output;

    cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
    cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;

    cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
    cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;

    cmd.ExecuteNonQuery();                                          /*       Execute the stored procedure                                 */

    Console.WriteLine(cmd.Parameters["@GameDate"].Value);
    Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
    Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
    Console.ReadLine(); 
}

修改

在查看更完整的代码后,这是您的错误

cmd.Parameters.AddWithValue("@TeamName", item.Text);
SqlDataReader reader = cmd.ExecuteReader();

执行@date时,您尚未添加@HomeTeam@AwayTeamSqlDataReader reader = cmd.ExecuteReader();参数,这就是您收到错误的原因。您应该在执行cmd.ExecuteReader()之前添加这三个参数。您还需要使用command.Parameters.Clear();清除cmd的参数,因为您在循环中使用cmd并删除cmd.ExecuteNonQuery(),因为您在调用时已经在执行存储过程cmd.ExecuteReader()。更改您的代码如下

if (item.Selected == true)
{
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@TeamName", item.Text);
    cmd.Parameters.Add("@date", SqlDbType.Date);
    cmd.Parameters["@date"].Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@HomeTeam", SqlDbType.VarChar, 25);
    cmd.Parameters["@HomeTeam"].Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@AwayTeam", SqlDbType.VarChar, 25);
    cmd.Parameters["@AwayTeam"].Direction = ParameterDirection.Output;
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(cmd.Parameters["@GameDate"].Value);
        Console.WriteLine(cmd.Parameters["@HomeTeam"].Value);
        Console.WriteLine(cmd.Parameters["@AwayTeam"].Value);
        Console.ReadLine(); 
    }