Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.net获取SQL Server存储过程结果_Vb.net_Sql Server 2008_Visual Studio 2012_Stored Procedures_Parameters - Fatal编程技术网

VB.net获取SQL Server存储过程结果

VB.net获取SQL Server存储过程结果,vb.net,sql-server-2008,visual-studio-2012,stored-procedures,parameters,Vb.net,Sql Server 2008,Visual Studio 2012,Stored Procedures,Parameters,我想获得存储过程从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; SELEC

我想获得存储过程从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
当我执行存储过程时,我会得到我想要的结果:

编辑:这是我当前的代码,感谢下面的建议:

    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
我甚至尝试过他的建议:

    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)
这两个代码都为MSSQLSCOMM.ExecuteOnQuery提供了一个未经处理的错误:

System.Data.dll中发生类型为“System.Data.SqlClient.SqlException”的未处理异常

其他信息:过程或函数“csp_LoginAuthentication”需要参数“@accountstatus”,但未提供该参数


我非常感谢大家的帮助。

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

 msSQLComm.Parameters.Add("@accountstatus", SqlDbType.TinyInt).Direction = ParameterDirection.InputOutput
 msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
我还建议更改AddWithValue方法。这是一条捷径,有许多缺点,你可以在书中读到

查看您的编辑,我注意到您没有执行命令。 在准备好命令文本、参数和设置连接后,您应该使用这些参数执行命令

 msSqlComm.ExecuteNonQuery()
之后,您的输出参数应该准备好了,您可以使用

 accntStat = Convert.ToInt16(msSQLComm.Parameters("@accountstatus").Value)
 loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
注意,我将读取参数值的命令放在Convert.toxxx调用中。如果使用编译,则不需要此选项,但如果将此选项设置为Off,则最好将其设置为ON。当编译器发出从一个数据类型到另一个数据类型的自动转换时,这将有助于避免细微的错误

最后,SqlCommand需要知道使用什么连接来访问数据库。如果生成命令时未指定连接对象,则该命令无法执行存储过程。可以将初始化命令的行更改为

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

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

 msSQLComm.Parameters.Add("@accountstatus", SqlDbType.TinyInt).Direction = ParameterDirection.InputOutput
 msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
我还建议更改AddWithValue方法。这是一条捷径,有许多缺点,你可以在书中读到

查看您的编辑,我注意到您没有执行命令。 在准备好命令文本、参数和设置连接后,您应该使用这些参数执行命令

 msSqlComm.ExecuteNonQuery()
之后,您的输出参数应该准备好了,您可以使用

 accntStat = Convert.ToInt16(msSQLComm.Parameters("@accountstatus").Value)
 loginCnt = Convert.ToInt32(msSQLComm.Parameters("@logincount").Value)
注意,我将读取参数值的命令放在Convert.toxxx调用中。如果使用编译,则不需要此选项,但如果将此选项设置为Off,则最好将其设置为ON。当编译器发出从一个数据类型到另一个数据类型的自动转换时,这将有助于避免细微的错误

最后,SqlCommand需要知道使用什么连接来访问数据库。如果生成命令时未指定连接对象,则该命令无法执行存储过程。可以将初始化命令的行更改为

Dim msSQLComm As SqlCommand = New SqlCommand("csp_LoginAuthentication", sqlConnct.MSSQLConn )
我认为这解决了几个问题,包括:输出参数和设置它们的方向,密码参数设置为整数(显然是某种字符串),以及在出现异常时无法关闭连接

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

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


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

谢谢大家帮我解决这个问题,我能够找出代码的问题。我已经检查并试着更换了

    msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
    msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
致:

现在它给了我想要的结果!再次感谢你们对我的帮助,尤其是史蒂夫和乔尔·科霍恩。以下是完整的代码,以防有人遇到同样的问题:

    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)

谢谢大家帮我解决了这个问题,我能够解决代码的问题。我已经检查并试着更换了

    msSQLComm.Parameters.Add("@accountstatus", SqlDbType.Int).Direction = ParameterDirection.InputOutput
    msSQLComm.Parameters.Add("@logincount", SqlDbType.Int).Direction = ParameterDirection.InputOutput
致:

现在它给了我想要的结果!再次感谢你们对我的帮助,尤其是史蒂夫和乔尔·科霍恩。以下是完整的代码,以防有人遇到同样的问题:

    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)

嗨,谢谢你的回复,我尝试了你的两个建议。它不再给我一个错误,但现在它给我的结果是0,而不是1。我将修改我的问题以显示我的代码。此外,我还尝试通过消息框msSQLComm.Parameters显示结果。Add@accountstatus,SqlDbType.Int.Direction=ParameterDirection.InputOutput,结果为False。您好,再次感谢您耐心地帮助我。我试过你的建议了

打开,但它为MSSQLSCOMM.ExecuteOnQuery提供了一个错误。我刚刚修改了上面的代码。我忘了提到,我在存储过程中将TINYINT从INT改为INT,并按照我想要的方式正确执行。我还按照@marc_的建议重命名了我的SPU。您能解释一下在ExecuteOnQuery行中收到了什么错误吗?应该在填充参数集合后调用ExecuteOnQuery,而不是之前。您好,感谢您的回复,我尝试了您的两个建议。它不再给我一个错误,但现在它给我的结果是0,而不是1。我将修改我的问题以显示我的代码。此外,我还尝试通过消息框msSQLComm.Parameters显示结果。Add@accountstatus,SqlDbType.Int.Direction=ParameterDirection.InputOutput,结果为False。您好,再次感谢您耐心地帮助我。我尝试了你的建议,但它给了我一个msSqlComm.ExecuteNonQuery错误。我刚刚修改了上面的代码。我忘了提到,我在存储过程中将TINYINT从INT改为INT,并按照我想要的方式正确执行。我还按照@marc_的建议重命名了我的SPU。您能解释一下ExecuteOnQuery行中出现了什么错误吗?ExecuteOnQuery应该在填充参数集合后调用,而不是之前。旁注:您不应该在存储过程中使用sp_uu前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用sp_u,并使用其他东西作为前缀——或者根本不使用前缀!完成,感谢开明的上帝,这是纯文本密码吗!?0_0@JoelCoehoorn:只是学习在VB.net>\u中调用SP如果您只是学习,那么值得一提的是,返回此类数据的正常方式是通过结果集,而不是输出参数。另外,请使用另一个示例,因为纯文本密码是各种不好的juju。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用sp_u,并使用其他东西作为前缀——或者根本不使用前缀!完成,感谢开明的上帝,这是纯文本密码吗!?0_0@JoelCoehoorn:只是学习在VB.net>\u中调用SP如果您只是学习,那么值得一提的是,返回此类数据的正常方式是通过结果集,而不是输出参数。另外,请使用另一个示例,因为纯文本密码是各种不好的juju-ju。感谢您的回复,我尝试了您的代码,但它给我的错误是System.Data.SqlClient.SqlException类型的未处理异常发生在System.Data.dll中。其他信息:过程或函数“csp_LoginAuthentication”需要参数“@accountstatus”,但该参数不受支持,因为这个参数显然包含在代码中。我刚刚在上面发布了我的更新代码。我也觉得很奇怪,因为我检查了一些文章,代码现在应该或必须工作,但它不是。我想我会哭>\u谢谢你的回复,我尝试了你的代码,但它给了我一个错误,System.Data.SqlClient.SqlException类型的未处理异常发生在System.Data.dll中。其他信息:过程或函数“csp_LoginAuthentication”需要参数“@accountstatus”,但它不受支持,因为这个参数显然包含在代码中。我刚刚在上面发布了我的更新代码。我也觉得很奇怪,因为我检查了一些文章,代码现在应该或必须工作,但它不是。我想我要哭了>_