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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值