1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[VB.net] SQLiteを読み込んでデータグリッドビューに表示するクラス

Last updated at Posted at 2019-08-31

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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?