【发布时间】:2015-05-01 14:13:14
【问题描述】:
我想将存储过程的结果(来自 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() 错误:
System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常
附加信息:过程或函数“csp_LoginAuthentication”需要参数“@accountstatus”,但未提供。
我真的很感谢大家在这里的帮助。
【问题讨论】:
-
旁注:您应该不为您的存储过程使用
sp_前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用其他东西作为前缀 - 或者根本不使用前缀! -
完成了,多谢赐教
-
亲爱的上帝,这是一个纯文本密码!? 0_0
-
@Joel Coehoorn: 刚刚学习在 VB.net 中调用 SP (>_
-
如果您只是学习,值得一提的是,返回此类数据的正常方式是通过结果集,而不是输出参数。另外,使用不同的示例,因为纯文本密码是各种糟糕的juju。
标签: vb.net sql-server-2008 visual-studio-2012 stored-procedures parameters