CREATE proc dbo.[usp_UpsertCustomer]
@CustomerID int, @CustomerName nvarchar(50), @CustomerAddress nvarchar(100),
@CustomerPhone nvarchar(50), @CustomerEmail nvarchar(50)
as
set nocount on
declare @initcount int = (select count(*) from dbo.Customer)
begin
merge dbo.Customer as c
using (select @CustomerID,@CustomerName,@CustomerAddress,@CustomerPhone,@CustomerEmail) as s
-- src data maps to the following fields
(CustomerID, CustomerName, CustomerAddress, CustomerPhone, CustomerEmail)
on c.CustomerID = s.CustomerID
when matched then --update the record
update set c.CustomerName = s.CustomerName, c.CustomerAddress = s.CustomerAddress,
c.CustomerPhone = s.CustomerPhone, c.CustomerEmail = s.CustomerEmail
when not matched then --insert the new record
insert (CustomerName,CustomerAddress,CustomerPhone,CustomerEmail)
values (s.CustomerName,s.CustomerAddress,s.CustomerPhone,s.CustomerEmail);
-- return ID of the new record if created
if @initcount < (select count(*) from dbo.Customer)
return (select max(CustomerID) from dbo.Customer)
else
return 0
end- c#代码
public class clsAutoInvoiceDb
{
private Database objDb = new DatabaseProviderFactory().CreateDefault();
public int CreateCustomer(string _CustomerName, string _CustomerAddress,
string _CustomerPhone, string _CustomerEmail)
{
DbCommand cmd = objDb.GetStoredProcCommand("usp_UpsertCustomer");
objDb.AddParameter(cmd, "@return_value", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);
objDb.ExecuteNonQuery("usp_UpsertCustomer", -1, _CustomerName, _CustomerAddress, _CustomerPhone, _CustomerEmail);
return Convert.ToInt32(objDb.GetParameterValue(cmd, "@return_value"));-我的问题/问题-我的问题/问题
插入总是成功的,但是当返回语句执行时,@return_value是空的。在我重新分解使用entlib之前,这个代码段很好。现在无法得到我的返回值。有人有什么想法吗?已经在这上面浪费了3+时间。
发布于 2016-05-11 08:04:55
我无意中发现了你的问题,我也有同样的痛苦。
我尝试过帖子所暗示的,但是它不起作用,而且在这方面有有限的官方指导。
然而,作为一个解决办法,我已经开始工作了(砖头,欢迎.-)
if @initcount < (select count(*) from dbo.Customer) select max(CustomerID) from dbo.Customer else select 0 endvar returnValue = db.ExecuteScalar("usp_UpsertCustomer",parameterArray);
希望它能帮助您和其他同行的企业库数据访问阻止用户。
https://stackoverflow.com/questions/21071677
复制相似问题