Top >
>
[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
System.Data.SQLiteを用いたDBプログラミング。
DBから値を取得するには、DataReaderとDataAdapterを用いた方法があるが、DataReaderを使った取得方法の自分メモ。
Dim SQLString As String
Dim MyConnectionString As String = "data source={0}"
Dim MySQLiteConnection As New SQLiteConnection
MyConnectionString = String.Format(MyConnectionString, _
System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Hoge.db"))
Try
MySQLiteConnection.ConnectionString = MyConnectionString
MySQLiteConnection.Open()
Debug.Print("DB Open")
SQLString = "SELECT * FROM EMP"
Dim MySQLiteCommand As New SQLiteCommand(SQLString, MySQLiteConnection)
Debug.Print(SQLString)
Dim MySQLiteDataReader As SQLiteDataReader
MySQLiteDataReader = MySQLiteCommand.ExecuteReader()
Dim i As Integer = 0
Do While MySQLiteDataReader.Read
Me.BoardDataGridView.Rows.Add()
Me.BoardDataGridView.Rows(i).Cells(0).Value = MySQLiteDataReader.GetValue(0)
Me.BoardDataGridView.Rows(i).Cells(1).Value = MySQLiteDataReader.GetValue(1)
i = i + 1
Loop
Catch ex As Exception
MessageBox.Show(ex.Message, "DB Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
MySQLiteConnection.Close()
Debug.Print("DB Close")
End Try
実際には、参考URLにあるように、Usingを用いたものの方が望ましいようだ。
Usingステートメントで簡単、確実にファイルをクローズ - @IT
VisualStudioにて、DBファイルを追加するには、次URLにあるように、データソースを追加した後、プロジェクトに既存項目追加で設定する。
[C#][SQLite]System.Data.SQLiteを試してみた
VSのデータソースからDataGrid追加を用いれば、上ソースのように、自分でDataGridに行を設定する必要がなく楽である。
(どちらの方が、パフォーマンス優位かは不明)
参考:
SQLiteを使ったWindowsアプリを作成する
VB.Netでソート2 - FloralCompany.log
PR

