1

我有一个名为的存储过程SelFromWeather2,我需要从表中返回值。当我执行语法检查时,我没有收到错误,但是当我从 C# 调用它时,我在 SelFromWeather2 附近遇到语法错误。

这是代码:

CREATE PROCEDURE SelFromWeather2  
@location VARCHAR(MAX),
@des VARCHAR(200) OUTPUT,
@min INT OUTPUT,
@max INT OUTPUT,
@humidity INT OUTPUT,
@pressure INT OUTPUT,
@speed INT OUTPUT,
@date Datetime OUTPUT
AS
IF EXISTS(SELECT * FROM Weather2 WHERE LOCATION LIKE @location)
BEGIN

 CREATE TABLE T
 (
 forc XML,
 loc VARCHAR(MAX),
 dat Datetime
 );

 INSERT INTO T(forc, loc, dat) SELECT TOP 1 [FORECAST],[LOCATION],[DATE] FROM Weather2   
 WHERE LOCATION LIKE @location ORDER BY DATE DESC;

 SET @location=(SELECT loc FROM T);
 SET @location =(SELECT loc FROM T);
 SET @des= (SELECT forc.value('(/Weather//Forecast/Description/node())[1]', 'nvarchar(max)')   FROM T);
 SET @min= (SELECT  forc.value('(/Weather//Forecast/MinTemp/node())[1]', 'int') FROM T);
 SET @max=(SELECT forc.value('(/Weather//Forecast/MaxTemp/node())[1]', 'int') FROM T);
 SET @humidity=(SELECT forc.value('(/Weather//Forecast/Humidity/node())[1]', 'int') FROM T);
 SET @pressure= (SELECT forc.value('(/Weather//Forecast/Pressure/node())[1]', 'int') FROM T);
 SET @speed=(SELECT forc.value('(/Weather//Forecast/Speed/node())[1]', 'int') FROM T);
 SET @date= (SELECT forc.value('(/Weather//Forecast/Date/node())[1]', 'Datetime') FROM T);

 DROP TABLE T;
END

调用这个过程的代码是:

           string location = "Paris";

            SqlDataReader myReader = null;
            SqlCommand myComand = new SqlCommand("SelFromWeather2",  myConnection);
            myComand.Parameters.AddWithValue("@location", location);

            SqlParameter min = myComand.Parameters.Add("@min", System.Data.SqlDbType.Int);
            SqlParameter max = myComand.Parameters.Add("@max", System.Data.SqlDbType.Int);
            SqlParameter humidity = myComand.Parameters.Add("@humidity", System.Data.SqlDbType.Int);

            SqlParameter pressure = myComand.Parameters.Add("@pressure", System.Data.SqlDbType.Int);
            SqlParameter speed = myComand.Parameters.Add("@speed", System.Data.SqlDbType.Int);
            SqlParameter dat = myComand.Parameters.Add("@date", System.Data.SqlDbType.DateTime);
            SqlParameter des = myComand.Parameters.Add("@des", System.Data.SqlDbType.VarChar, 200);
            min.Direction = System.Data.ParameterDirection.Output;               
            max.Direction = System.Data.ParameterDirection.Output;
            humidity.Direction = System.Data.ParameterDirection.Output;
            pressure.Direction = System.Data.ParameterDirection.Output;
            speed.Direction = System.Data.ParameterDirection.Output;
            dat.Direction = System.Data.ParameterDirection.Output;
            des.Direction = System.Data.ParameterDirection.Output;

            myComand.ExecuteReader();


            int minTemp = (int)min.Value;
            int maxTemp = (int)max.Value;
            int hum = (int)humidity.Value;
            int press = (int)pressure.Value;
            int wind = (int)speed.Value;
            string description = des.Value.ToString();
            DateTime datum = (DateTime)dat.Value;

请帮忙,我卡住了!

4

2 回答 2

4

您需要CommandTypeStoredProcedure

myCommand.CommandType = CommandType.StoredProcedure

默认值为文本。

于 2013-09-10T01:20:36.077 回答
4

CommandTypemyCommand 设置为StoredProcedure

于 2013-09-10T01:20:28.637 回答