2

我已经阅读并尝试以多种方式重建以下内容,但为了清楚起见,我将展示我的最后一次尝试。

目标 - 获取列“UniqueID”的最大值

列字段 'uniqueID' 设置为 bigint

我假设错误与 addwithvalue in 一致,因为我得到“int”作为返回值

如果我运行查询SELECT MAX(UniqueID) FROM tblResults in SQL它可以工作

代码

 Dim connection5 As SqlConnection
    Dim command5 As New SqlCommand
    Dim ds5 As New DataSet
    Dim ConnectionString5 As String = System.Configuration.ConfigurationManager.ConnectionStrings("mySQLConnectionString").ToString()
    connection5 = New SqlConnection(ConnectionString5)

    connection5.Open()
    command5.Connection = connection5
    command5.Parameters.Clear()

    command5.CommandText = "spUniqueUserID"
    command5.Parameters.AddWithValue("@UniqueID", SqlDbType.BigInt)
    command5.Parameters("@UniqueID").Direction = ParameterDirection.Output
    command5.CommandType = CommandType.StoredProcedure
    command5.ExecuteNonQuery()

    Session.Item("UniqueID") = command5.Parameters("@UniqueID").Value
    connection5.Close()

    Dim vShow As String
    vShow = ""
    vShow = Session.Item("UniqueID").ToString

SP

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[spUniqueUserID]    Script Date: 09/10/2013 08:51:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spUniqueUserID]

@UniqueID bigint OUTPUT

AS

BEGIN

select @UniqueID = (SELECT MAX(UniqueID) FROM tblResults )

Return @UniqueID

END
4

3 回答 3

4

我个人不会弄乱输出参数。只需使用

ALTER PROCEDURE [dbo].[spUniqueUserID]

AS

BEGIN

 SELECT MAX(UniqueID) FROM tblResults

END

在你的过程中,和

Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
Dim returnValue As Object

cmd.CommandText = "spUniqueUserID"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlConnection1

sqlConnection1.Open()

returnValue = cmd.ExecuteScalar()

sqlConnection1.Close()

在你的代码中。(最好使用连接和命令的语句,为简洁起见,此处省略)

于 2013-09-10T09:17:58.183 回答
1

尝试

 command5.Parameters.AddWithValue("@UniqueID", 0L)

AddWithValue 确定要传递给底层引擎的参数的 DataType,查看第二个参数的数据类型。你传递一个字符串,这肯定是错误的。

或者,您可以明确定义参数

 Dim parameter = new SqlParameter("@UniqueID", SqlDbType.BigInt)
 parameter.Direction = ParameterDirection.Output
 parameter.Size = 8
 parameter.Value = 0
 command5.Parameters.Add(parameter)

并且,作为最后但基本的步骤,不要忘记指定此命令执行存储过程,然后执行命令

 command5.CommandType = CommandType.StoredProcedure
 command5.ExecuteNonQuery()

作为一种替代方法,但这需要更改您的存储过程,是使用 ExecuteScalar。当您需要代码中的单个结果时,应使用此方法。

ALTER PROCEDURE [dbo].[spUniqueUserID]
AS

BEGIN
    select SELECT MAX(UniqueID) FROM tblResults
END

在你的代码中

    Using connection5 = New SqlConnection(ConnectionString5)
    Using command5 As New SqlCommand("spUniqueUserID", connection5)
         connection5.Open()
         command5.CommandType = CommandType.StoredProcedure
         Dim result = command5.ExecuteScalar()
         .....
    End Using
    End Using

但是此时存储过程的用处确实很小,您可以直接在 SqlCommand 的构造函数中编写 sql 并删除 CommandType 设置

    Using connection5 = New SqlConnection(ConnectionString5)
    Using command5 As New SqlCommand("SELECT MAX(UniqueID) FROM tblResults", connection5)
         connection5.Open()
         Dim result = command5.ExecuteScalar()
         .....
    End Using
    End Using
于 2013-09-10T09:15:13.900 回答
0
 Dim ConnectionString5 As String = System.Configuration.ConfigurationManager.ConnectionStrings("mySQLConnectionString").ToString()

        Using connection5 = New SqlConnection(ConnectionString5)
            Using command5 As New SqlCommand("SELECT MAX(UniqueID) FROM tblResults", connection5)

                connection5.Open()
                Dim result = command5.ExecuteScalar()

                Session.Item("UniqueID") = result
                connection5.Close()
            End Using
        End Using

        Dim vShow As String
        vShow = ""
        vShow = Session.Item("UniqueID").ToString
于 2013-09-10T10:35:44.803 回答