【问题标题】:VB.net getting SQL Server stored procedure resultsVB.net 获取 SQL Server 存储过程结果
【发布时间】: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


【解决方案1】:
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)

我认为这解决了几个问题,包括:输出参数和设置它们的方向,密码参数设置为整​​数,显然是某种字符串,以及在异常情况下无法关闭连接。


我对原始代码也有一个严重的担忧:它确实看起来那里有一个纯文本密码。你只是不这样做。这不行,需要立即修复。

【讨论】:

  • 感谢您的回复,我尝试了您的代码,但它给了我错误An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dllAdditional information: Procedure or function 'csp_LoginAuthentication' expects parameter '@accountstatus', which was not supplied
  • 奇怪,因为代码中清楚地包含了那个参数。
  • 我刚刚在上面发布了我的更新代码。我也觉得很奇怪,因为我检查了一些文章并且代码现在应该(或必须)工作,但事实并非如此。我想我要哭了(>_
【解决方案2】:

您还应该将输出参数添加到参数集合中

 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。当编译器发出从 DataType 到另一个 DataType 的自动转换时,这将有助于避免细微的错误

最后,SqlCommand 需要知道使用什么连接来访问数据库。如果您在未分配连接对象的情况下构建命令,则该命令无法执行存储过程。您可以将初始化命令的行更改为

Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn )

【讨论】:

  • 您好,感谢您的回复,我尝试了您的两个建议。它不再给我一个错误,但现在它给了我“0”而不是“1”。我将修改我的问题以显示我的代码。
  • 另外,我尝试通过消息框( msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput )显示结果,它给了我“False as a结果。
  • 您好,再次感谢您耐心地帮助我。我尝试了您的建议,但它给了我 msSqlComm.ExecuteNonQuery() 的错误。我刚刚修改了上面的代码。我忘了提到我已经在我的存储过程中将 TINYINT 从 INT 更改为我想要的方式正确执行。我还按照@marc_s 的建议重命名了我的 SP
  • 您能解释一下您在 ExecuteNonQuery 行中收到什么错误吗?
  • ExecuteNonQuery 应该在您填充参数集合之后而不是之前调用。
【解决方案3】:

感谢大家帮助我解决问题,我能够找出代码的问题。我检查了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

现在它给了我想要的结果!再次感谢您帮助我,尤其是 SteveJoel 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2011-08-31
    • 1970-01-01
    • 2018-07-13
    • 2014-03-07
    • 2014-07-21
    • 1970-01-01
    相关资源
    最近更新 更多