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

私はそれを少し変更し、すべてのdbプロセスを関数に追加し、email変数を関数に渡そうとしましたが、電子メールアドレスを出力していますが、関数に送信していません。これが新しいコードです。

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 に答える