SQLite専用の読み込みクラスを作りました。
引数には以下の文を指定して下さい。
まずはNuGetパッケージから、Sqlite.Dataをインストールして下さい。
path = SQLiteデータベースファイルのパス
table = 対象のテーブル
insertSQL =SQL文
datagrid = Datagridviewオブジェクト
fillStr = フィルターを書けるワード
以下コードです。
Imports System.Data.SQLite
Public Class SQLiteClass
Public Sub SQL_Execution(path As String, table As String, insertSQL As String, datagrid As DataGridView)
'SQL文の実行メソッド
Debug.Print(insertSQL)
'Try
Using con As New SQLiteConnection("Data Source=" & path)
con.Open()
Using SqlCommand As SQLiteCommand = con.CreateCommand()
SqlCommand.CommandText = insertSQL
SqlCommand.ExecuteNonQuery()
End Using
End Using
'Fast_data_show(path, table, datagrid)
'Catch ex As Exception
' MsgBox("エラー", vbAbort)
'End Try
End Sub
Public Sub Fast_data_show(path As String, table As String, datagrid As DataGridView)
'データグリッドビューにデータを表示
' Try
datagrid.Columns.Clear()
Using con As New SQLiteConnection("Data Source=" & path)
Dim ds As New DataSet
Dim da As New SQLiteDataAdapter("SELECT * FROM " & table, con)
da.Fill(ds, table)
datagrid.DataSource = ds.Tables(table)
con.Close()
con.Dispose()
End Using
'Catch ex As Exception
' MsgBox("エラー", vbAbort)
'End Try
End Sub
Public Sub DataGridViewFilter(fillStr As String, datagrid As DataGridView, sqlStr As String)
'データグリッドビューにフィルターをかける
Dim objBind As BindingSource
Dim objData As DataTable
' DataGridViewにバインドしたデータをDataTableとして取得する
objData = CType(datagrid.DataSource, DataTable)
' データが存在する場合
If IsNothing(objData) = False Then
objBind = New BindingSource
' バインドするデータソースにDataTableをセット
objBind.DataSource = objData
' データソースにフィルターをセット
objBind.Filter = sqlStr
End If
End Sub
End Class