将值从 SQL Server 返回到 VB.Net

Posted

技术标签:

【中文标题】将值从 SQL Server 返回到 VB.Net【英文标题】:Returning a value from SQL Server to VB.Net 【发布时间】:2021-01-08 03:01:07 【问题描述】:

如何将@attackPercentage 传递回我的 VB 程序并让读者阅读?我不断收到错误消息:

System.IndexOutOfRangeException: 攻击百分比

代码:

ALTER PROCEDURE [dbo].[SelectPlayersRating]
    @playerAccountID uniqueidentifier,
    @raterAccountID uniqueidentifier
AS
BEGIN
    DECLARE @attackPercentage INT

    SELECT attack, safety, consistency 
    FROM tblRatings 
    WHERE @playerAccountID = playerAccountID 
      AND @raterAccountID = raterAccountID

    SET @attackPercentage = '99' --Test Value

    RETURN @attackPercentage 
END

VB.NET 代码:

Dim DBConnect3 As New DBConn

Using db As DbConnection = DBConnect3.Conn("DBConnectionString")

    Dim cmd As SqlCommand = DBConnect3.Command(db, "SelectPlayersRating")
    cmd.Parameters.Add(New SqlParameter("playerAccountID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = Guid.Parse(Request.QueryString("aID"))
    cmd.Parameters.Add(New SqlParameter("raterAccountID", SqlDbType.Uniqueidentifier, ParameterDirection.Input)).Value = acc.accountID

    db.Open()

    Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

    While DR.Read
        ddlAttack.SelectedValue = DR("attack")
        ddlSafety.SelectedValue = DR("safety")
        ddlConsistency.SelectedValue = DR("consistency")
        session("test") = DR("attckPercentage")
    End While

    DR.Close()
    DR = Nothing
    cmd.Dispose()
    cmd = Nothing
    db.Dispose()
    db.Close()
End Using

【问题讨论】:

【参考方案1】:

根据您的存储过程,您的查询可以返回多个结果,因此您的 While DR.Read 语句。

但是,每次返回存储过程时,attackPercentage 的值都会返回一个值。因此,您可以使用输出参数从存储过程中提取值:

ALTER PROCEDURE [dbo].[SelectPlayersRating]
@playerAccountID uniqueidentifier,
@raterAccountID uniqueidentifier
, @attachPercentage INT output --  This is the new output parameter
AS
BEGIN
-- DECLARE @attackPercentage INT -- remove the declaration as we've defined it already

SELECT attack, safety, consistency 
FROM tblRatings 
WHERE @playerAccountID = playerAccountID 
  AND @raterAccountID = raterAccountID

SET @attackPercentage = '99' --Test Value

-- RETURN @attackPercentage  -- No need to return this value
END

那么现在,你如何获得返回值呢?只需将输出参数添加到您的参数集合:

  Dim cmd As SqlCommand = DBConnect3.Command(db, "SelectPlayersRating")
cmd.Parameters.Add(New SqlParameter("playerAccountID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = Guid.Parse(Request.QueryString("aID"))
cmd.Parameters.Add(New SqlParameter("raterAccountID", SqlDbType.Uniqueidentifier, ParameterDirection.Input)).Value = acc.accountID
cmd.Parameters.Add(New SqlParameter("attackPercentage", SqlDbType.Int, ParameterDirection.Output))

调用 ExecuteReader 后,您可以简单地说:

session("test") = cmd.Parameters("attackPercentage").Value

【讨论】:

【参考方案2】:

我删除了返回值。

ALTER PROCEDURE [dbo].[SelectPlayersRating]

    @playerAccountID uniqueidentifier,
    @raterAccountID uniqueidentifier

    AS
    BEGIN

        SELECT attack, safety, consistency FROM tblRatings WHERE        playerAccountID = @playerAccountID AND 
    raterAccountID = @raterAccountID  

    END

这显然是一个网络应用程序。我目前恰好在 WinForms 中,但这在 Web 应用程序中是相同的,只是不同的事件签名。只需将参数值发送给函数,然后使用返回的数据表。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt = GetAttackData(Guid.Parse(Request.QueryString("aID")), acc.accountID)
    ddlAttack.SelectedValue = dt(0)("attack")
    ddlSafety.SelectedValue = dt(0)("safety")
    ddlConsistency.SelectedValue = dt(0)("consistency")
    session("test") = 99 'You can calculate this here instead of the stored procedure
End Sub

我不确定您为什么要为 Sql Server 使用 DbConnection。不要从其他地方获得连接。需要关闭和释放连接,因此请使用局部变量。

Private Function GetAttackData(aId As Guid, accID As Guid) As DataTable
    Using dt As New DataTable
        Using db As New SqlConnection(ConStr),
        cmd As New SqlCommand("SelectPlayersRating", db)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("playerAccountID", SqlDbType.UniqueIdentifier).Value = aId
            cmd.Parameters.Add("raterAccountID", SqlDbType.UniqueIdentifier).Value = accID
            db.Open()
            Using DR As SqlDataReader = cmd.ExecuteReader()
                dt.Load(DR)
            End Using
        End Using
        Return dt
    End Using
End Function

【讨论】:

以上是关于将值从 SQL Server 返回到 VB.Net的主要内容,如果未能解决你的问题,请参考以下文章

错误:表插入在 vb.net 和 msacess 中返回错误

SQL根据条件将值从一列复制到另一列

C# SQL 命令参数,将值从表单“传输”到对象再到方法

如何正确地将值从 pyqt 返回到 JavaScript?

将值从 c# 绑定到返回 NaN 的 html?

SQL将值从一个表列更新到另一个[重复]