Yas*_*ser 7 c# database sql-server asp.net
我正在使用输出参数从我的数据库中获取值.
这是我的存储过程:
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
Run Code Online (Sandbox Code Playgroud)
我不确定最后的回报:
RETURN (@SubscriptionPIN)
RETURN (@SignupDate)
RETURN (@ProductCount)
Run Code Online (Sandbox Code Playgroud)
另一方面,这是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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到错误:
System.InvalidOperationException:String [1]: 在System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc,
System.SataClient)的System.Data.SqlClient.SqlParameter.Validate(Int32 index,Boolean isCommandProc)中
,Size属性的大小为0 . System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean )
中的System.Data.SqlClient.SqlCommand.BuildRPC(布尔inSchema,SqlParameterCollection参数,_SqlRPC和rpc)
中的Int32 startCount,Boolean inSchema,SqlParameterCollection参数)系统
中的System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法,DbAsyncResult结果)
处于系统的System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,String methodName,Boolean sendToPipe)
中的async).Data.SqlClient.SqlCommand.ExecuteNonQuery()
我不知道从存储过程发送许多输出参数的正确方法是什么,有人可以帮忙吗?
您需要指定nvarchar
参数的最大长度:
SqlParameter SubscriptionPIN = new SqlParameter("@TheCustomerID", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SubscriptionPIN);
SqlParameter SignupDate = new SqlParameter("@SignupDate", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(SignupDate);
Run Code Online (Sandbox Code Playgroud)
return
从存储过程中删除语句。您无需执行任何操作即可返回输出参数。(此外,您只能使用一个return
语句,并且只能返回整数值。您可以使用带有方向的参数ReturnValue
来获取返回值。)
归档时间: |
|
查看次数: |
33159 次 |
最近记录: |