首页 文章

使用vb.net oledbcommand从存储过程中检索输出参数(标识列)

提问于
浏览
0

我试图从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;

请指教 .

2 回答

  • 0

    首先,为什么使用执行读取器功能?如果您不想从存储中返回任何值,则只需使用 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();
    

    在您的存储过程中不需要这些输出参数之后 .

  • 1

    使用与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
    

相关问题