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

您需要两件事:每条记录之间的换行符和格式字符串中的长度信息(拍摄固定宽度的列)。

'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 回答