【发布时间】:2014-08-19 05:18:09
【问题描述】:
我正在尝试从 SQL Server 2008 R2 中的存储过程中检索输出参数值(标识字段)。
我查看了以下 stackoverflow 链接。我也在做同样的事情,但仍然面临问题: Retrieving output parameter from stored procedure with oledb command vb.net Stored procedure - return identity as output parameter or scalar
我的存储过程:
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;
请指教。
【问题讨论】:
-
1.如果微软打算废弃它,为什么还要使用 OleDB? - 使用
SqlClient。 2.尝试设置out参数的大小。 -
也试过了。我还尝试将其设置为 4。我尝试将其作为方向定义的单独行以及参数添加行的一部分。结果相同。使用 VS 2008 .net 3.5
-
我没有注意到,但你确实有“ExecuteReader”...使用“ExecuteNonQuery”,你会没事的
标签: sql vb.net stored-procedures sql-server-2008-r2