【发布时间】:2015-06-29 21:46:26
【问题描述】:
我试图将单个输入参数传递给一个存储过程,我认为该存储过程声明了 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 的输出变量仅充当输出变量而不是输入/输出变量,以便存储过程不期望从它们获得输入值?
【问题讨论】:
-
很多关于如何在互联网和 Stackoverflow 上执行此操作的示例..您是否尝试过通过更改进行谷歌搜索..?
-
我做了,但我一直在搜索asp.net存储过程的输出参数。在定义输出参数与输入/输出参数的方式上,我没有发现任何与我的代码不同的地方。你有什么建议吗?
标签: c# asp.net sql-server stored-procedures