2011-12-05

使用VB.net列出SQLite資料的範例(select)


********************************************

    Private Sub ListData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListData.Click
        Try
            Dim zSQLFile As String = "Data Source=C:\SQLite檔案存放資料夾\NewDB3.db3"
            '連接資料庫
            Dim oConn As New SQLiteConnection(zSQLFile)
            oConn.Open()
            '執行SQL指令
            Dim zSQL As String = "SELECT * from test "
            Dim oCmd As SQLiteCommand = New SQLiteCommand(zSQL, oConn)
            '將執行結果放到DataReader
            Dim oDR As SQLiteDataReader = oCmd.ExecuteReader()
            '將Dataview的資料顯示在ListBox1上
            ListBox1.Items.Clear()
            While oDR.Read()
                'ListBox1.Items.Add(String.Format("{0},{1},{2}", oDR(0), oDR(1), oDR(2)))
                ListBox1.Items.Add(String.Format("{0},{1},{2}", oDR("oid"), oDR("word"), oDR("denotation")))
            End While
            lblMsg.Text = "Data Added to ListBox ready!!!"
            oCmd.Dispose()
            oConn.Close()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try
    End Sub

沒有留言:

張貼留言