初嘗C# 連接本地數據存儲 SQLite
一:安裝
SQLITE,是一款輕型的數據庫,是遵守ACID的關聯式數據庫管理系統。我直接使用的是http://sqlite.phxsoftware.com/(An open source ADO.NET provider for the SQLite database engine),下載完畢是一個EXE。
然后引用 System.Data.SQLite.dll 程序集;
如果你還想在使用SQLite 中同時使用Linq,則還需要引用 System.Data.SQLite.Linq.dll 程序集;
二:新建數據庫
安裝完畢后,打開visual studio,新建數據連接,可以看到數據源多了一項SQLite。
新建連接,如下圖。SQLITE的數據庫,保存后是一個文件。
三:數據庫維護
可以在VS中方面的維護SQLITE數據,如下圖:
可以在VS中使用類似SQL查詢分析器的功能,如下圖:
四:混合模式
安裝完畢,可以直接在項目集的引用中,多了
System.Data.SQLite
System.Data.SQLite.Linq
兩個程序集,由于http://sqlite.phxsoftware.com/的System.Data.SQLite是混合模式程序集,是針對“v2.0.50727”版的運行時生成的,在沒有配置其他信息的情況下,無法在 4.0 運行時中加載該程序集。故需要在App.config中配置如下參數。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
五:我的測試代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
namespace xg_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//連接字符串參考
//string strCon = "Datasource=Test.db3;Pooling=true;FailIfMissing=false";
//"NorthwindEF (SQLite)" connectionString="provider=System.Data.SQLite;metadata=Schemas\NorthwindEFModel.csdl|Schemas\NorthwindEFModel.msl|Schemas\NorthwindEFModel.SQLite.ssdl;Provider Connection String='Data Source=DB\northwindEF.db'"
string strCon = "Data Source=xg_DataBase;Pooling=true;password=sa";
SQLiteConnection con = new SQLiteConnection(strCon);
SQLiteCommand cmd = new SQLiteCommand("select * from student", con);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
namespace xg_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//連接字符串參考
//string strCon = "Datasource=Test.db3;Pooling=true;FailIfMissing=false";
//"NorthwindEF (SQLite)" connectionString="provider=System.Data.SQLite;metadata=Schemas\NorthwindEFModel.csdl|Schemas\NorthwindEFModel.msl|Schemas\NorthwindEFModel.SQLite.ssdl;Provider Connection String='Data Source=DB\northwindEF.db'"
string strCon = "Data Source=xg_DataBase;Pooling=true;password=sa";
SQLiteConnection con = new SQLiteConnection(strCon);
SQLiteCommand cmd = new SQLiteCommand("select * from student", con);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
學習來源:C# 數據本地存儲方案之 SQLite
| 作者: XuGang 網名:鋼鋼 |
| 出處: http://xugang.cnblogs.com |
| 聲明: 本文版權歸作者和博客園共有。轉載時必須保留此段聲明,且在文章頁面明顯位置給出原文連接地址! |




浙公網安備 33010602011771號