0

我的变量有问题。我通过来自外部程序的参数接收变量 email 的值。在 For Next 循环中时,它包含正确的值,但当它退出循环时,它突然没有值了。我将如何退出循环并维护 email 变量的值。

Option Strict On

Imports MySql.Data.MySqlClient
Imports System.Runtime.Serialization

Module jrConnect

    Sub Main(ByVal cmdArgs() As String)
        Dim cs As String = "serverinfo"
        Dim conn As New MySqlConnection(cs)
        Dim entID As String
        Dim email As String

        Dim returnValue As Integer = 0

        If cmdArgs.Length > 0 Then
            For argNum As Integer = 0 To UBound(cmdArgs, 1)
                Console.Write("your email address is " & cmdArgs(argNum))
                email = cmdArgs(argNum)
                'value of email is set
            Next argNum

        End If

        Try

            conn.Open()
            Console.Write("Connected")
            Dim stm As String = "SELECT ###### FROM ###### WHERE email =" & "'" & email & "'"
            'the email variable at this point has no value

            Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
            Dim reader As MySqlDataReader = cmd.ExecuteReader()

            While reader.Read()
                entID = reader.GetString(0)
            End While
            reader.Close()

            Dim stm2 = "SELECT value FROM ###### WHERE ###### = " & entID
            Dim cmd2 As MySqlCommand = New MySqlCommand(stm2, conn)
            Dim reader2 As MySqlDataReader = cmd2.ExecuteReader()
            Dim counter As Integer = 0

            While reader2.Read() And counter < 3
                Console.WriteLine(reader2.GetString(0) & "%")
                counter = counter + 1
            End While
            reader.Close()

        Catch ex As MySqlException
        Finally
            conn.Close()
        End Try

    End Sub

End Module

我得到的错误...... http://hostthenpost.org/uploads/a66f3efbf2eba24cef0f4e8536111b54.png

我对其进行了一些更改,并将所有数据库进程添加到一个函数中,并尝试将电子邮件变量传递给该函数,但它正在打印电子邮件地址但不将其发送到该函数。这是新代码。

Option Strict Off

Imports MySql.Data.MySqlClient
Imports System.Runtime.Serialization

Module jrConnect

    Sub Main(ByVal cmdArgs() As String)
        Dim temp As String = "spkelly86@gmail.com"
        'MsgBox("The Main procedure is starting the application.")
        Dim returnValue As Integer = 0
        ' See if there are any arguments.
        If cmdArgs.Length > 0 Then
            temp = cmdArgs(0)
            Console.Write(temp & " IN VB!")
            DBConnect(temp)
        End If
        ' Insert call to appropriate starting place in your code.
        'MsgBox("The application is terminating.")




    End Sub

    Function DBConnect(ByVal email As String)

        Dim cs As String = "serverinfo"
        Dim conn As New MySqlConnection(cs)
        Dim entID As String
        Try

            conn.Open()
            Dim stm As String = "SELECT ****** FROM ******** WHERE email =" & "'" & email & "'"
            Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
            Dim reader As MySqlDataReader = cmd.ExecuteReader()

            While reader.Read()
                entID = reader.GetString(0)
            End While
            reader.Close()

            Dim stm2 = "SELECT value FROM ****** WHERE ****** = " & entID
            Dim cmd2 As MySqlCommand = New MySqlCommand(stm2, conn)
            Dim reader2 As MySqlDataReader = cmd2.ExecuteReader()
            Dim counter As Integer = 0

            While reader2.Read() And counter < 3
                Console.WriteLine(reader2.GetString(0) & "%")
                counter = counter + 1
            End While
            reader.Close()

        Catch ex As MySqlException
        Finally
            conn.Close()
        End Try
        Return 0
    End Function
End Module
4

1 回答 1

2

设置变量时,您不会退出循环。

尝试这个:

Dim email As String = "dummy@email.com"
For argNum As Integer = 0 To UBound(cmdArgs, 1)
    Console.Write("your email address is " & cmdArgs(argNum))
    email = cmdArgs(argNum)
    'value of email is set
    Exit For
Next argNum

或等价物:

Dim email As String = "dummy@email.com"
If cmdArgs.Length > 0 Then
    Console.Write("your email address is " & cmdArgs(0))
    email = cmdArgs(0)
End If
于 2012-10-18T23:54:52.397 回答