1、Slite简介

SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。
至今已经有13个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

2、在C#中连接Sqlite

连接Sqlite首先需要添加System.Data.SQLite.dll和System.Data.SQLite.Linq.dll的引用,这两个dll文件你可以根据你的操作系统版本选择合适的安装版本,安装完成之后的文件路径为C:\Program Files\System.Data.SQLite\2008\bin。

添加了上面所说的两个引用之后,为方便调用,写了一个SqlHelper类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace ConsoleSqlite
{
    public class DBHelper
    {
        public SQLiteConnection GetCon()
        {
            string strFilePath = @"Data Source=C:\test.db";
            string strCon = strFilePath + ";Pooling=true;FailIfMissing=false";
            SQLiteConnection sqliteCon = new SQLiteConnection(strCon);
            return sqliteCon;
        }
    }

    public class SqlHelper : DBHelper
    {
        private DataTable dt;
        private SQLiteConnection conn;
        private SQLiteCommand cmd;
        private SQLiteDataAdapter sda;
        /// <summary>
        /// 数据库操作类
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int RunSQL(string sql)
        {
            int count = 0;
            try
            {
                conn = GetCon();
                conn.Open();
                cmd = new SQLiteCommand(sql, conn);
                cmd.ExecuteNonQuery();
                count = count + 1;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return count;
        }
        /// <summary>
        /// 获得datatable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            DataSet ds = null;
            try
            {
                conn = GetCon();
                sda = new SQLiteDataAdapter(sql, conn);//OracleDataAdapter:网络适配器
                ds = new DataSet();
                sda.Fill(ds);//将结果填充到ds中
                dt = ds.Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
        /// <summary>
        /// 返回记录总条数
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public int GetCount(string strTableName)
        {
            string strSql = "select count(*) from " + strTableName;
            int count = 0;
            DataTable dtCount = GetDataTable(strSql);
            count = int.Parse(dtCount.Rows[0][0].ToString());
            return count;
        }
    }
}

上面的类中,包含了基本的操作,一般人是够用了,为了测试我的类建立的是否正确,我新建了一个控制台程序,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleSqlite
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlHelper helper = new SqlHelper();
            string strSql = "Select * From county";
            DataTable dt = helper.GetDataTable(strSql);
            Console.WriteLine("Hello");
        }
    }
}

在中间设置断点进行调试,发现是对的,大功告成!


附件:

C#连接Sqlite的更多相关文章

  1. VS2010连接SQLite数据库

    Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:This is the only setu ...

  2. Java 连接SQLite数据库

    下载jar包: http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz public class TestSQLite { ...

  3. FIREDAC连接SQLITE乱码的解决

    在好多群里面都碰到问“FIREDAC连接SQLITE乱码的”的问题的同仁,遂将解决方法贴出来: 如上图所示设置 stringFormat为unicode即可

  4. C#连接SQLite的字符串

    一.C#在不同情况下连接SQLite字符串格式 1.Basic(基本的) Data Source=filename;Version=3; 2.Using UTF16(使用UTF16编码) Data S ...

  5. Python3实现连接SQLite数据库的方法

    本文实例讲述了Python3实现连接SQLite数据库的方法,对于Python的学习有不错的参考借鉴价值.分享给大家供大家参考之用.具体方法如下: 实例代码如下: ? 1 2 3 4 5 6 7 8 ...

  6. C#.net连接SQLite及遇到的问题

    1.Slite简介 SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需 ...

  7. 一步一步教你用c# entity framework6 连接 sqlite 实现增删改查

    使用entity framework6 连接 SQLite 数据库 前言 很多小型应用程序中,都要使用数据库,而现在比较流行的本地数据库非SQLite莫属. 第一步:前期准备 开发环境:vs2015 ...

  8. 一起学微软Power BI系列-使用技巧(6) 连接Sqlite数据库

    好久没有研究Power BI了,看到高飞大神弄的东西,太惭愧了.今天有个小东西,数据在Sqlite里面,想倒腾到Power BI Desktop里面折腾一下,结果发现还不直接支持.所以只好硬着头皮上去 ...

  9. unity3d连接Sqlite并打包发布Android

    连接Sqlite首先要把dll程序集导入到unity3d工程里面.安装好的unity中可以找到

  10. VS2010上连接SQLite数据库

    VS2010连接SQLite数据库 Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:Thi ...

随机推荐

  1. xshell下载安装

    打开网址http://www.netsarang.com/download/software.html 找到最新版的xshell,点击下载 在跳转的页面填写个人信息,许可证类型选择家庭和学校使用,除了 ...

  2. java.text.SimpleDateFormat使用介绍

    java.text.SimpleDateFormat的使用 java.lang.Object   |   +----java.text.Format           |           +-- ...

  3. WCF经典代码

    Array.CreateInstance(typeof(object), methodCall.Args.Length) 1. DataContractSerializer支持的类型......... ...

  4. CNN学习笔记:目标函数

    CNN学习笔记:目标函数 分类任务中的目标函数 目标函数,亦称损失函数或代价函数,是整个网络模型的指挥棒,通过样本的预测结果与真实标记产生的误差来反向传播指导网络参数学习和表示学习. 假设某分类任务共 ...

  5. netstat 与 telnet

    在网络方面我们常常会用到如下命令: (1)ping命令:我们常常用来判断2台或2台以上的机器间是否网络连通. ping 192.168.1.88 -t 如果想看任何命令的参数是什么意思,我们只需要:命 ...

  6. 单例模式及getInstance()的用法

    一般在单例模式下使用.getInstance()创建对象:但并不是所有有私有构造方法,对外通过getInstance方法提供 实例的情况就是单例模式. 注:单例模式:一个类有且只有一个实例.1,一个私 ...

  7. LeetCode: Find Largest Value in Each Tree Row

    BFS /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...

  8. LigerUI LigerGird IE7/8 下不显示 “in”的操作数无效

    LigerUI IE7/8 下显示 in的操作数无效 修改脚本生成LigerGrid的地方,将最后一列后面的,去掉

  9. InterruptedException异常

    本文总结自:https://blog.csdn.net/asdfsadfasdfsa/article/details/78808131 什么样的方法会抛出InterruptedException异常? ...

  10. MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...