初嘗C# 串連本機資料儲存 SQLite

來源:互聯網
上載者:User

 

一:安裝

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;
        }
    }
}

 

學習來源:C# 資料本機存放區方案之 SQLite

 

Demo 下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.