首页 > 解决方案 > 我怎样才能找回这个?

问题描述

这是我的存储过程:

BEGIN
SET NOCOUNT ON;

INSERT INTO [dbo].[KYS_CustomerComplaints] (
    CustomerId
    ,UserId
    ,STATE
    ,Address
    ,PhoneNumber
    ,Email
    ,StorageCondition
    ,OtherConditions
    ,ProductId
    ,ProductAmount
    ,LOTNo
    ,HandBill
    ,CallingBill
    ,ProductionDate
    ,ExpirationDate
    ,CreatedDate
    ,LastEditionDate
    ,LastEdited_UserId
    ,Code
    )
VALUES (
    @CustomerId
    ,@UserId
    ,@State
    ,@Address
    ,@PhoneNumber
    ,@Email
    ,@StorageCondition
    ,@OtherConditions
    ,@ProductId
    ,@ProductAmount
    ,@LOTNo
    ,@HandBill
    ,@CallingBill
    ,@ProductionDate
    ,@ExpirationDate
    ,@CreatedDate
    ,@LastEditionDate
    ,@LastEdited_UserId
    ,@Code
    )

DECLARE @new_identity INT;

SELECT @new_identity = SCOPE_IDENTITY()

RETURN @new_identity;
END

如何提供输出 ID?

var parameters = new[]
{
    new SqlParameter("@CustomerId",customer.CustomerId ),
    new SqlParameter("@UserId",customer.UserId),
    new SqlParameter("@State",customer.State ),
    new SqlParameter("@Address",customer.Address ),
    new SqlParameter("@PhoneNumber",customer.PhoneNumber ),
    new SqlParameter("@Email",customer.Email ),
    new SqlParameter("@StorageCondition",customer.StorageCondition ),
    new SqlParameter("@OtherConditions",customer.OtherConditions ),
    new SqlParameter("@ProductId",customer.ProductId ),
    new SqlParameter("@ProductAmount",customer.ProductAmount ),
    new SqlParameter("@LOTNo",customer.LOTNo ),
    new SqlParameter("@HandBill",customer.HandBill ),
    new SqlParameter("@CallingBill",customer.CallingBill ),
    new SqlParameter("@ProductionDate",customer.ProductionDate ),
    new SqlParameter("@ExpirationDate",customer.ExpirationDate ),
    new SqlParameter("@CreatedDate",customer.CreatedDate ),
    new SqlParameter("@LastEditionDate",customer.LastEditionDate ),
    new SqlParameter("@LastEdited_UserId",customer.LastEdited_UserId ),
    new SqlParameter("@Code",customer.Code ),
};

var model = AdoNetHelper.ExecuteSql(ProcType.Scalar, ReturnType.DataSet, "CRM_CustomerComplaint_Add", parameters);

return "Başarıyla Eklendi";

标签: asp.netsql-server

解决方案


第一种方法:像这样添加一个输出参数

create procedure myStoredProc(@input1 int, ..., @NewIdentity int output)
as
begin
     set nocount on;
     insert into KYS_CustomerComplaints(...)  values (...);
     SELECT @NewIdentity = SCOPE_IDENTITY();
end

第二种方法:让过程像这样返回值

create procedure myStoredProc(@input1 int, ...)
as
begin
     set nocount on;
     insert into KYS_CustomerComplaints(...)  values (...);
     SELECT SCOPE_IDENTITY();
end

return在您的情况下,声明没有用处。

更多信息在这里
https://docs.microsoft.com/en-us/sql/relational-databases/stored-procedures/return-data-from-a-stored-procedure?view=sql-server-2017

在这里
使用 C# 中的存储过程输出参数


推荐阅读