我知道这个问题已经被问过很多次了,并且我已经阅读了很多很多答案,但我无法弄清楚什么是错的。已经好几个小时了。任何帮助将不胜感激。我只是想在 ASP 页面中调用存储过程,但无法正确添加参数,得到它不在集合中的异常。
我已按如下方式修改了我的过程,以尝试使其简单并隔离问题。
ALTER PROCEDURE [dbo].[up_validateUserWithClinicCount]
@HTID INT = 0,
@ValidUserID INT OUTPUT,
@MultiClinicFlag INT OUTPUT
AS
DECLARE @vClinicCount INT = null
DECLARE @vUserValid INT = null
BEGIN
SET @ValidUserID = 2
SET @MultiClinicFlag = 1
END;
和 C# 代码
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PC3PaymentConnection"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("up_validateUserWithClinicCount", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@HTID", htId);
SqlParameter uidOut = new SqlParameter("@ValidUserID", SqlDbType.Int);
uidOut.Size = 4;
uidOut.Direction = ParameterDirection.Output;
cmd.Parameters.Add(uidOut);
SqlParameter pMultiClinics = new SqlParameter();
pMultiClinics.ParameterName = "@MultiClinicFlag";
pMultiClinics.SqlDbType = SqlDbType.Int;
pMultiClinics.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pMultiClinics);
try
{
cmd.ExecuteNonQuery();
//--> Error points to the next line, and I have tried to use int.parse rather than convert also, with the same error -- parameter not in collection
MultiClinics = Convert.ToInt16(cmd.Parameters["pMultiClinics"].Value);
PC3User = Convert.ToInt16(uidOut.Value.ToString());
}
catch (SqlException sqlEx)
{
LbMsg.ForeColor = System.Drawing.Color.Red;
LbMsg.Text = sqlEx.Message;
}
}
}
谢谢,如果你能看到我错过了什么。