我正在使用输出参数从我的数据库中获取值。
这是我的存储过程:
ALTER PROCEDURE [dbo].[sp_GetCustomerMainData]
-- Add the parameters for the stored procedure here
@Reference nvarchar(100),
@SubscriptionPIN nvarchar(100) OUTPUT,
@SignupDate nvarchar(100) OUTPUT,
@ProductCount int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET @SubscriptionPIN = 'N/A'
SET @SignupDate = 'N/A'
SET @ProductCount = 0
-- Insert statements for procedure here
IF EXISTS(SELECT [SubscriptionPIN] FROM [Norton].[dbo].[Customers] WHERE [Reference] = @Reference)
BEGIN
SELECT TOP 1 @SubscriptionPIN = [SubscriptionPIN], @SignupDate = SignUpDate FROM [Norton].[dbo].[ProductList] WHERE [Reference] = @Reference
SET @ProductCount = (SELECT COUNT(*) FROM [Norton].[dbo].[ProductList] WHERE [Reference] = @Reference)
END
RETURN (@SubscriptionPIN)
RETURN (@SignupDate)
RETURN (@ProductCount)
END
我不确定最后的回报:
RETURN (@SubscriptionPIN)
RETURN (@SignupDate)
RETURN (@ProductCount)
另一方面,这里是 c# 代码:
using (SqlConnection con = new SqlConnection(connectionInfo))
{
using (SqlCommand cmd = new SqlCommand("sp_GetCustomerMainData", con) { CommandType = CommandType.StoredProcedure })
{
cmd.Parameters.Add("@Reference", SqlDbType.NVarChar).Value = CustomerReferenceID;
SqlParameter SubscriptionPIN = new SqlParameter("@TheCustomerID", SqlDbType.NVarChar) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SubscriptionPIN);
SqlParameter SignupDate = new SqlParameter("@SignupDate", SqlDbType.NVarChar) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SignupDate);
SqlParameter ProductCount = new SqlParameter("@ProductCount", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(ProductCount);
con.Open();
try
{
cmd.ExecuteNonQuery();
if (cmd.Parameters["@TheCustomerID"].Value.ToString() != "N/A")
{
aStatus.SubscriptionPIN = cmd.Parameters["@TheCustomerID"].Value.ToString();
aStatus.SignupDate = cmd.Parameters["@SignupDate"].Value.ToString();
aStatus.ProductCount = int.Parse(cmd.Parameters["@ProductCount"].Value.ToString());
aStatus.Result = "0: Reference ID Found";
}
else
{
aStatus.Result = "1: Reference ID does not exists";
return aStatus;
}
}
catch (SqlException sqlExc)
{
foreach (SqlError error in sqlExc.Errors)
{
aStatus.Result = string.Format("{0}: {1}", error.Number, error.Message);
return aStatus;
}
}
}
}
当我运行此代码时,出现错误:
System.InvalidOperationException:字符串 [1]:Size 属性在 System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)处的大小属性为0.Data.SqlClient.SqlCommand.SetUpRPCParameters
(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String 方法, DbAsyncResult 结果)
在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult 结果,字符串方法名,布尔 sendToPipe)
在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
我不知道从存储过程发送许多输出参数的正确方法是什么,有人可以帮忙吗?