自動翻訳(Auto Translation)
- English
- 日本語
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」をインストーラーすることで、解決することができます。
