【VB.net】SQLiteConnectionでOpenに失敗するときの対処法

自動翻訳(Auto Translation)

VB.netでSQLiteを使用し、データベースにアクセスしようとしたとき、データベースに接続できないエラーが発生することがあります。

VB.net
Imports Microsoft.Data.Sqlite
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' コネクション作成 Using con = New SQLiteConnection(GetConnectionString()) con.Open() Using cmd = con.CreateCommand() ' テーブル作成SQL cmd.CommandText = "CREATE TABLE users (" & "id INTEGER PRIMARY KEY," & "name TEXT NOT NULL," & "age INTEGER," & "email TEXT NOT NULL UNIQUE" & ")" cmd.ExecuteNonQuery() End Using End Using
End Sub
''' <summary>
''' 接続文字列を取得します。
''' </summary>
Private Function GetConnectionString() As String ' Return New SQLiteConnectionStringBuilder() With {.DataSource = "Common.db"}.ToString()
End Function
ユーザーが処理していない例外

System.Exception: ‘You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().’

対処法

データベースに接続できない利用は、必要なパッケージがインストールされておらず、「SQLitePCL.Batteries.Init()」が実行されていないためです。

NuGetパッケージにて、「SQLitePCLRaw.bundle_e_sqlite3」をインストーラーすることで、解決することができます。

    この記事は役に立ちましたか?


    広告ブロック機能を検知しました

    このウェブサイトは広告収益によって成り立っています。ご協力をお願いします。