我已阅读并重试以下方面重建以下内容,但为了清楚说明,我将展示我的最后一次尝试。
目标 - 获取列“UniqueID”的最大值
列字段'uniqueID'设置为bigint
我认为错误与addwithvalue一致,因为我得到“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
答案 0 :(得分: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()
在你的代码中。 (最好使用连接和命令的语句,为简洁起见,此处缺失)
答案 1 :(得分: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)
并且,作为最后但基本步骤,不要忘记指定此命令执行storedprocedure然后执行命令
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
但是在这一点上,storedprocedure的实用性非常小,您可以直接在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
答案 2 :(得分: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