Visual Studio 2022 System.Data.SQLite 套件
原本使用 Mircosoft.EntityFrameworkCore.Sqlite 來連結 SQLite 資料庫,結果因為兩個問題放棄使用
- 關聯 dll 太多
- 沒有 SQLiteDataAdapter 類別
所以還是使用網路上大部分人使用的 System.Data.SQLite
直接產生 DataTable 丟到 datagridview 馬上就好了,使用 SQLiteDataReader 還要自己弄 DataTabl 感覺多做工又沒效率,然後看看產生出來的 dll ...恩,少很多了 XD
以下是自己的程式碼,有需要的就直接拿去吧
private void use_SQLiteDataAdapter()
{
string strFileName = txtSQLite_Path.Text;
string strConnect = "Data Source=" + strFileName + ";";
SQLiteConnection myConnection = new SQLiteConnection(strConnect);
myConnection.Open();
SQLiteCommand myCommand = new SQLiteCommand();
//列出資料
string strSQL = "";
strSQL = "SELECT * FROM " + listBox1.SelectedItem.ToString();
myCommand.CommandText = strSQL;
myCommand.Connection = myConnection;
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(myCommand);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
myConnection.Close();
}
留言
張貼留言