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

发布时间: 2017/3/26 3:43:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想将单个输入的参数传递到存储过程,,我以为,有输出参数声明,但似乎是输入/输出变量,从而给了我一条错误消息,指出其中一个参数不提供一个值。

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:

您的存储的过程有 @teamname 参数设置为输入的参数,但你不任何的值设置为 @teamname 参数在 C# 代码中,所以这就是为什么你得到了错误。您需要将值设置为 @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 ,和 @AwayTeam 参数时你做 SqlDataReader reader = cmd.ExecuteReader(); ,那你为什么有错误。你应该添加这三个参数之前执行 cmd.ExecuteReader() 。你也需要清除的参数 cmd 使用 command.Parameters.Clear(); 既然您使用 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(); 
    }
官方微信
官方QQ群
31647020