VB.net 正在获取 SQL 服务器存储过程结果
VB.net getting SQL Server stored procedure results
我想将存储过程(来自 SQL Server 2008)的结果导入 VB.net 2012。
这是我的存储过程:
CREATE PROCEDURE [dbo].[csp_LoginAuthentication]
@employeeid VARCHAR (20),
@password VARCHAR (20),
@accountstatus INT OUT,
@logincount INT OUT
AS
BEGIN
SET NOCOUNT ON;
SELECT
@accountstatus = account_status,
@logincount = logincount
FROM
strato_blueapplesmis.dbo.app_login
WHERE
1 = 1
AND employee_id = @employeeid
AND password = @password
END
当我执行我的存储过程时,我得到了我想要的结果:
已编辑:这是我当前的代码,感谢下面的 Steve's 建议:
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
.Open()
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.VarChar).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.VarChar).Value = txtPassword.Text
Dim accntStat As Integer
Dim loginCnt As Integer
accntStat = Convert.ToInt32(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
msSQLComm.ExecuteNonQuery()
MsgBox(accntStat)
End With
我什至尝试了 Joel Coehoorn 的建议:
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.NVarChar, 20).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.NVarChar, 20).Value = txtPassword.Text
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
sqlConnct.MSSQLConn.Open()
msSQLComm.ExecuteNonQuery()
accntStat = CInt(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = CInt(msSQLComm.Parameters("@logincount").Value)
End With
MsgBox(accntStat)
这两个代码都给我一个未处理的错误 msSQLComm.ExecuteNonQuery()
:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Procedure or function 'csp_LoginAuthentication' expects parameter '@accountstatus', which was not supplied.
在此非常感谢大家的帮助。
您还应该将输出参数添加到参数集合中
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.TinyInt).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
我还建议更改 AddWithValue 方法。这是一个有许多缺点的快捷方式,您可以在 Can We stop using AddWithValue already?
中阅读
查看您的编辑,我注意到您没有执行命令。
准备好命令文本、参数并设置连接后,您应该使用
执行命令
msSqlComm.ExecuteNonQuery()
之后您的输出参数应该准备就绪,您可以使用
检索它们
accntStat = Convert.ToInt16(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
请注意,我将读取参数的 Value
放在 Convert.ToXXXX
调用中。如果您使用 Option Strict Off 编译,则不需要这样做,但是,如果您将此选项设置为 Off,则最好将其设置为 ON。当编译器发出从一种数据类型到另一种数据类型的自动转换时,这将有助于避免细微的错误
最后,SqlCommand 需要知道使用什么连接来访问数据库。如果您在未分配连接对象的情况下构建命令,则该命令无法执行存储过程。您可以将初始化命令的行更改为
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn )
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
Using sqlConnct.MSSQLConn, _
cmd As New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@employeeid", SqlDbType.Int).Value = txtEmployeeID.Text
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 40).Value = txtPassword.Text
cmd.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
cmd.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
sqlConnct.MSSQLConn.Open()
cmd.ExecuteNonQuery()
accntStat = CInt(cmd.Parameters("@accountstatus").Value)
loginCnt = CInt(cmd.Parameters("@logincount").Value)
End Using
MsgBox(accntStat)
我认为这解决了几个问题,包括:输出参数和设置它们的方向,密码参数设置为整数,显然是某种字符串,以及在出现异常时无法关闭连接。
我对原始代码也有严重的担忧:看起来确实有纯文本密码。你只是不那样做。这不行,需要立即.
修复
感谢大家帮我解决了问题,我能够找出代码的问题。我检查了 Procedure or function “” expects parameter “”, which was not supplied 并尝试替换
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
收件人:
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.Output
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.Output
现在它给了我想要的结果!再次感谢您帮助我,尤其是 Steve 和 Joel Coehoorn。这是完整的代码,以防有人遇到同样的问题:
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.NVarChar).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtPassword.Text
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.Output
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.Output
sqlConnct.MSSQLConn.Open()
msSQLComm.ExecuteNonQuery()
accntStat = CInt(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = CInt(msSQLComm.Parameters("@logincount").Value)
End With
MsgBox(loginCnt)
我想将存储过程(来自 SQL Server 2008)的结果导入 VB.net 2012。
这是我的存储过程:
CREATE PROCEDURE [dbo].[csp_LoginAuthentication]
@employeeid VARCHAR (20),
@password VARCHAR (20),
@accountstatus INT OUT,
@logincount INT OUT
AS
BEGIN
SET NOCOUNT ON;
SELECT
@accountstatus = account_status,
@logincount = logincount
FROM
strato_blueapplesmis.dbo.app_login
WHERE
1 = 1
AND employee_id = @employeeid
AND password = @password
END
当我执行我的存储过程时,我得到了我想要的结果:
已编辑:这是我当前的代码,感谢下面的 Steve's 建议:
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
.Open()
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.VarChar).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.VarChar).Value = txtPassword.Text
Dim accntStat As Integer
Dim loginCnt As Integer
accntStat = Convert.ToInt32(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
msSQLComm.ExecuteNonQuery()
MsgBox(accntStat)
End With
我什至尝试了 Joel Coehoorn 的建议:
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.NVarChar, 20).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.NVarChar, 20).Value = txtPassword.Text
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
sqlConnct.MSSQLConn.Open()
msSQLComm.ExecuteNonQuery()
accntStat = CInt(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = CInt(msSQLComm.Parameters("@logincount").Value)
End With
MsgBox(accntStat)
这两个代码都给我一个未处理的错误 msSQLComm.ExecuteNonQuery()
:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Procedure or function 'csp_LoginAuthentication' expects parameter '@accountstatus', which was not supplied.
在此非常感谢大家的帮助。
您还应该将输出参数添加到参数集合中
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.TinyInt).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
我还建议更改 AddWithValue 方法。这是一个有许多缺点的快捷方式,您可以在 Can We stop using AddWithValue already?
中阅读查看您的编辑,我注意到您没有执行命令。
准备好命令文本、参数并设置连接后,您应该使用
msSqlComm.ExecuteNonQuery()
之后您的输出参数应该准备就绪,您可以使用
检索它们 accntStat = Convert.ToInt16(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
请注意,我将读取参数的 Value
放在 Convert.ToXXXX
调用中。如果您使用 Option Strict Off 编译,则不需要这样做,但是,如果您将此选项设置为 Off,则最好将其设置为 ON。当编译器发出从一种数据类型到另一种数据类型的自动转换时,这将有助于避免细微的错误
最后,SqlCommand 需要知道使用什么连接来访问数据库。如果您在未分配连接对象的情况下构建命令,则该命令无法执行存储过程。您可以将初始化命令的行更改为
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn )
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
Using sqlConnct.MSSQLConn, _
cmd As New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@employeeid", SqlDbType.Int).Value = txtEmployeeID.Text
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 40).Value = txtPassword.Text
cmd.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
cmd.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
sqlConnct.MSSQLConn.Open()
cmd.ExecuteNonQuery()
accntStat = CInt(cmd.Parameters("@accountstatus").Value)
loginCnt = CInt(cmd.Parameters("@logincount").Value)
End Using
MsgBox(accntStat)
我认为这解决了几个问题,包括:输出参数和设置它们的方向,密码参数设置为整数,显然是某种字符串,以及在出现异常时无法关闭连接。
我对原始代码也有严重的担忧:看起来确实有纯文本密码。你只是不那样做。这不行,需要立即.
修复感谢大家帮我解决了问题,我能够找出代码的问题。我检查了 Procedure or function “” expects parameter “”, which was not supplied 并尝试替换
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
收件人:
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.Output
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.Output
现在它给了我想要的结果!再次感谢您帮助我,尤其是 Steve 和 Joel Coehoorn。这是完整的代码,以防有人遇到同样的问题:
Dim accntStat As Integer
Dim loginCnt As Integer
Dim sqlConnct As New classSQLConnection
If sqlConnct.MSSQLConn.State = ConnectionState.Open Then sqlConnct.MSSQLConn.Close()
With sqlConnct.MSSQLConn
Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn)
msSQLComm.CommandType = CommandType.StoredProcedure
msSQLComm.Parameters.Add("@employeeid", SqlDbType.NVarChar).Value = txtEmployeeID.Text
msSQLComm.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtPassword.Text
msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.Output
msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.Output
sqlConnct.MSSQLConn.Open()
msSQLComm.ExecuteNonQuery()
accntStat = CInt(msSQLComm.Parameters("@accountstatus").Value)
loginCnt = CInt(msSQLComm.Parameters("@logincount").Value)
End With
MsgBox(loginCnt)