我正在尝试调用以RaceDate
作为输入并返回Location
作为输出的sql存储过程。我不确定如何在ASP.NET中调用我的代码,这就是我到目前为止所拥有的。
DateTime RaceDate = Calendar1.SelectedDate;
// string RaceDate = TxtBoxCalendar.Text;
TxtBoxCalendar.ReadOnly = true;
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ToString();
con.Open();
SqlCommand Command = new SqlCommand();
Command.CommandType = System.Data.CommandType.StoredProcedure;
Command.CommandText = "CheckRaceCalendarDates";
Command.Parameters.Add("@RaceDate", SqlDbType.DateTime, RaceDate);
Command.Parameters.Add("@Location", SqlDbType.String).Direction = ParameterDirection.Output;
Command.Parameters.Add("@Location",SqlDbType.String).Direction = ParameterDirection.Output;
Command.ExecuteNonQuery();
con.Close();
我认为我也可能会遇到数据类型问题。 RaceDate
是用户单击日历的日期,必须将其转换为字符串,但是SQL参数RaceDate
的类型为date。
CREATE PROCEDURE [dbo].[CheckRaceCalendarDates]
@RaceDates DATE,
@Location NVARCHAR(50) OUTPUT
AS
IF EXISTS
(
SELECT
RaceCalendar.RaceDates,
Locations.LocationName
FROM
Locations
INNER JOIN RaceCalendar ON locations.LocationId = RaceCalendar.LocationId
WHERE
RaceCalendar.RaceDates = @RaceDates
)
BEGIN
SELECT
@Location = Locations.LocationName
FROM
Locations
INNER JOIN RaceCalendar ON locations.LocationId = RaceCalendar.LocationId
WHERE
RaceCalendar.RaceDates = @RaceDates
END
答案 0 :(得分:2)
关于使用参数名称的问题;您已经在存储过程中使用了@RaceDates
,但是您尝试在代码中使用了@RaceDate
。它们应该相同。
此外,您需要像这样在代码中添加第二个参数;
Command.Parameters.Add("@Location",SqlDbType.String).Direction = ParameterDirection.Output;
在cmd.ExeCuteNonQuery();
之后
string location = Command.Parameters["@Location"].Value.ToString();