[英]Calling SQL Server stored procedure from VB.NET application. Output parameter is coming back blank
[英]Retrieve output parameter (identity column) from stored procedure with vb.net oledbcommand
我正在嘗試從SQL Server 2008 R2中的存儲過程中檢索輸出參數值(標識字段)。
我看了下面的stackoverflow鏈接。 我正在做同樣的事情,但仍然面臨問題: 使用oledb命令vb.net從存儲過程中檢索輸出參數 存儲過程-將標識作為輸出參數或標量返回
我的存儲過程:
ALTER proc [dbo].[sp_Custom_InsertxRef]
@DocumentID int,
@RevNr int,
@xRefDocument int,
@xRefRevNr int,
@xRefProjectId int,
@RefCount int,
@xRef int OUTPUT
AS
BEGIN
SET NOCOUNT ON
DECLARE @HasFullPath bit=1;
DECLARE @RelativePath nvarchar(300)='';
DECLARE @RefCountEdit float=NULL;
DECLARE @RefTimeStamp as datetime=GETDATE();
DECLARE @xrType as int = 1;
INSERT INTO [EpiGrid].[dbo].[XRefs]
([DocumentID]
,[RevNr]
,[XRefDocument]
,[XRefProjectID]
,[XRefRevNr]
,[HasFullPath]
,[RelativePath]
,[RefCount]
,[RefCountEdit]
,[RefTimeStamp]
,[XrType])
VALUES
(
@DocumentID,
@RevNr,
@xRefDocument,
@xRefProjectId,
@xRefRevNr,
@HasFullPath,
@RelativePath,
@RefCount,
@RefCountEdit,
@RefTimeStamp,
@xrType
)
SET @xRef=(SELECT SCOPE_IDENTITY());
--select @xRef=(SELECT SCOPE_IDENTITY());
return @xRef;
END
我的vb.net代碼:
Using connection As New System.Data.OleDb.OleDbConnection(Connectionstring)
connection.Open()
Using command As New OleDbCommand("sp_Custom_InsertxRef", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@DocumentID", OleDbType.Integer, 4, ParameterDirection.Input).Value = epdmParDoc.ID
command.Parameters.Add("@RevNr", OleDbType.Integer, 4, ParameterDirection.Input).Value = epdmParDoc.GetLocalVersionNo(parFolderId)
command.Parameters.Add("@xRefDocument", OleDbType.Integer, 4, ParameterDirection.Input).Value = targetReplaceDoc.ID
command.Parameters.Add("@xRefRevNr", OleDbType.Integer, 4, ParameterDirection.Input).Value = targetReplaceDoc.CurrentVersion
command.Parameters.Add("@xRefProjectId", OleDbType.Integer, 4, ParameterDirection.Input).Value = parFolderId
command.Parameters.Add("@RefCount", OleDbType.Integer, 4, ParameterDirection.Input).Value = count
'command.Parameters.Add("@xRef", OleDbType.Integer, 4, ParameterDirection.InputOutput).Value = -1
command.Parameters.Add("@xRef", OleDbType.Integer)
command.Parameters("@xRef").Direction = ParameterDirection.Output
command.ExecuteReader()
xRefId = command.Parameters("@xRef").Value
End Using
connection.Close()
End Using
我嘗試將方向作為輸出和返回值,但結果卻相同。 記錄是在數據庫中創建的(沒有錯誤),但是我的代碼對於輸出參數沒有任何幫助。 知道為什么嗎?
當我在SQL Server Management Studio中調試存儲過程時,它將創建記錄,並且我看到具有正確值的返回或輸出(均嘗試)。
Exec [dbo].[sp_Custom_InsertxRef]
@DocumentID =12,
@RevNr =1,
@xRefDocument = 15,
@xRefRevNr =1,
@xRefProjectId =1,
@RefCount =2,
@xRef=-1;
請指教。
首先,為什么要使用執行讀取器功能? 如果您不想從存儲中返回任何值,則只需使用ExecuteNonQuery()
方法而不是ExecuteReader()
。 如果要從存儲過程中讀取值,則可以使用ExecuteReader()
但必須已分配DataReader
對象。
例如
Dim dr As OleDBDataReader = Nothing
Try
dr = command.ExecuteReader()
IF dr.HasRows Then
While (dr.Read())
var value = dr.getValue(0)
End While
End IF
dr.Close()
Catch
'OleDB Exception will be thrown here
Finally
IF dr IsNot Nothing Then
IF dr.IsClosed = false Then dr.Close()
End If
End Try
但是,要使用上述方法,您應該在存儲過程中進行一些更改。
替換以下行:
SET @xRef=(SELECT SCOPE_IDENTITY());
--select @xRef=(SELECT SCOPE_IDENTITY());
return @xRef;
至
SELECT SCOPE_IDENTITY();
在存儲過程中不需要這些輸出參數之后。
使用與sqlclient相同的SP(無更改)而不是oledb可以完成此工作。 可能是錯誤。
Using connection As New System.Data.SqlClient.SqlConnection(connectionstrng)
connection.Open()
Using command As New System.Data.SqlClient.SqlCommand("sp_Custom_InsertxRef", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@DocumentID", SqlDbType.Int, 4, ParameterDirection.Input).Value = epdmParDoc.ID
command.Parameters.Add("@RevNr", SqlDbType.Int, 4, ParameterDirection.Input).Value = epdmParDoc.GetLocalVersionNo(parFolderId)
command.Parameters.Add("@xRefDocument", SqlDbType.Int, 4, ParameterDirection.Input).Value = targetReplaceDoc.ID
command.Parameters.Add("@xRefRevNr", SqlDbType.Int, 4, ParameterDirection.Input).Value = targetReplaceDoc.CurrentVersion
command.Parameters.Add("@xRefProjectId", SqlDbType.Int, 4, ParameterDirection.Input).Value = parFolderId
command.Parameters.Add("@RefCount", SqlDbType.Int, 4, ParameterDirection.Input).Value = count
'command.Parameters.Add("@xRef", OleDbType.Integer, 4, ParameterDirection.InputOutput).Value = -1
command.Parameters.Add("@xRef", SqlDbType.Int)
command.Parameters("@xRef").Direction = ParameterDirection.Output
command.ExecuteReader()
xRefId = command.Parameters("@xRef").Value
End Using
connection.Close()
End Using
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.