XuGang

记录一个程序员的成长

 

初尝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。

clip_image004

新建连接,如下图。SQLITE的数据库,保存后是一个文件。

clip_image006

 

三:数据库维护

可以在VS中方面的维护SQLITE数据,如下图:

clip_image008

可以在VS中使用类似SQL查询分析器的功能,如下图:

clip_image010

 

四:混合模式

安装完毕,可以直接在项目集的引用中,多了

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 下载

posted on 2011-04-19 11:51  钢钢  阅读(12241)  评论(2编辑  收藏  举报

导航