[英]How to get a SQL Server stored procedure return value using pyodbc?
[英]?? how to get stored procedure return value using enterprise library 6?
----------程序代碼----------------------------
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語句時@return_value為null。 在我重構為使用entlib之前,使用此代碼段的生活還不錯。 現在無法獲得我的返回值。 有人有想法么? 已經浪費了3個多小時。
我偶然發現了你的問題,我也有同樣的痛苦。
我已經嘗試過帖子提示的內容,但是它不起作用,並且在此方向上的官方指導非常有限。
但是,作為一種解決方法,我已經開始使用它了(歡迎brickbats ...-))
在sql中,而不是return中,請使用select返回值。 所以就你而言
if @initcount < (select count(*) from dbo.Customer) select max(CustomerID) from dbo.Customer else select 0 end
然后在C#中執行此操作
var returnValue = db.ExecuteScalar("usp_UpsertCustomer",parameterArray);
希望它能幫助您和其他企業庫數據訪問塊用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.