您好我尝试在输出值为的sql中创建sp
这是代码
ALTER PROCEDURE [dbo].[usp_SelectHospital_IfExiste_Department]
@HospitalDepartmentID INT,
@IfExiste INT OUTPUT
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
IF NOT EXISTS (SELECT c.DeptID FROM Clinic c WHERE DeptID=@HospitalDepartmentID )
BEGIN
SET @IfExiste=0
SELECT
[HospitalDepartmentID],
[NAME]
FROM
[dbo].[Hospital_Department]
WHERE
[HospitalDepartmentID] = @HospitalDepartmentID
END
ELSE
BEGIN
SET @IfExiste=1
SELECT
[HospitalDepartmentID],
[NAME]
FROM
[dbo].[Hospital_Department]
WHERE
[HospitalDepartmentID] = @HospitalDepartmentID
END
和C#代码
public static Hospital_Department GetDepartmentInfo(int ID,int OutIfExist)
{
SqlCommand cmd;
SqlDataReader dr;
Hospital_Department HD = new Hospital_Department();
using (cmd = new SqlCommand("usp_SelectHospital_IfExiste_Department", ProjectCon.GetCon()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@HospitalDepartmentID", ID);
//cmd.Parameters.Add("@IfExiste",SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@IfExiste",SqlDbType.Int);
cmd.Parameters["@IfExiste"].Direction = ParameterDirection.Output;
dr = cmd.ExecuteReader();
while (dr.Read())
{
HD.NAME = dr["NAME"].ToString();
HD.HospitalDepartmentID = Convert.ToInt32(dr["HospitalDepartmentID"]);
}
OutIfExist = Convert.ToInt32(cmd.Parameters["@IfExiste"].Value);
return HD;
}
}
当我尝试获取输出值时始终为null 我在sql中运行存储过程运行并返回值 所以请告诉我代码中的错误 thx
答案 0 :(得分:1)
也许这个question有用的信息:
根据http://msdn.microsoft.com/en-us/library/ms971497,您必须在处理输出参数之前关闭datareader。
希望它有所帮助。