1

わかりました、ここに私のコードがあります:

Imports IBM.Data.DB2.iSeries

Module ConsoleData1
Sub Main()

    'program identifier (to verify that you're using the right module)
    Console.WriteLine("ConsoleData1.vb")

    'create the connection object to the IBM i
    Using cn As New iDB2Connection("DataSource=xxxxxxxxxxxxx")
        Try
            cn.Open()
        Catch ex As iDB2Exception
            Console.WriteLine("An error occurred on cn.Open()")
            Exit Sub
        End Try

        'create a command object, initialize to valid SQL statement
        Using cmd As New iDB2Command("select * from testlib.finishk", cn)

            'create a command object, initialize to valid SQL statement
            Console.Write("Enter the Color Code!==>")
            Dim baldue = Console.ReadLine()

            Dim sql As String = "Select * from testlib.finishk where FINCOD >=" & FINCOD


            Using cmdx As New iDB2Command(sql, cn)
            End Using

            'create a data reader object, fill by executing the command
            Try
                Using dr As iDB2DataReader = cmd.ExecuteReader

                    'display data in a table
                    Console.Write("FINCOD" & vbTab)
                    Console.Write("FINDES" & vbTab)

                    While dr.Read()
                        Console.Write("{0}{1}", dr("FINCOD"), vbTab)
                        Console.Write("{0}{1}", dr("FINDES"), vbTab)
                    End While
                End Using

            Catch ex As iDB2Exception
                Console.WriteLine("An error occurred on cmd.ExecuteReader()")
                Exit Sub
            End Try
        End Using
    End Using
    Console.WriteLine("Press ENTER to end")
    Console.ReadLine()
End Sub

Sub HandleError(ByVal errorOccurred As String, ByVal ex As iDB2Exception)

    Console.WriteLine(errorOccurred)
    Console.WriteLine("Source: {0}", ex.Source)
    Console.WriteLine("SQLState: {0}", ex.SqlState)
    Console.WriteLine("Message: {0}", ex.Message)
    Console.WriteLine("MessageCode: {0}", ex.MessageCode)
    Console.WriteLine("MessageDetails: {0}", ex.MessageDetails)
    Console.ReadLine()
End Sub

Private Function Screen() As Object
    Throw New NotImplementedException
End Function

End Module

では、cmd ウィンドウのサイズを大きくして、テーブル構造の整​​合性を維持できるようにするにはどうすればよいでしょうか。cmd ウィンドウのサイズを変更すると、誰かが値をウィンドウにランダムに配置したように見えてしまいます。

ありがとう。

4

1 に答える 1

1

各レコード間の改行とフォーマット文字列の長さ情報の 2 つが必要です (固定幅の列を撮影するため)。

'You can play the -30 numbers here to get different spacing, based on the nature of your data
Dim recordFormat As String = "{0,-30} {1,-30}"

Console.WriteLine(recordFormat, "FINCOD","FINDES")
While dr.Read()
    Console.WriteLine(recordFormat, dr("FINCOD"), dr("FINDES"))
End While

最後に、私は DB2 に詳しくありませんが、ユーザーの FINDCOD 値を SQL ステートメントに追加するための文字列連結よりも優れた方法があることを教えてください。あなたがしていることは、恐ろしく許しがたいほど安全ではありません。0;DROP TABLE testlib.finishk;--FINDCOD の代わりにテキストをシステムに入れたらどうなりますか? 適切なコードは、これにパラメーター化されたクエリを使用します。

于 2012-12-18T20:48:20.160 に答える