C#连接Sqlite

1、Slite简介

SQLite,是一款轻型的,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、、等,还有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);//DataAdapter:网络适配器
                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");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C#连接SQLite数据库可以使用System.Data.SQLite库,这是一个SQLite3的ADO.NET数据提供程序。以下是连接SQLite数据库的示例代码: ```csharp using System.Data.SQLite; // 设置连接字符串,指定SQLite数据库文件的路径 string connectionString = @"Data Source=C:\mydatabase.db;Version=3;"; // 创建SQLite连接对象 SQLiteConnection connection = new SQLiteConnection(connectionString); // 打开数据库连接 connection.Open(); // 执行SQL语句 SQLiteCommand command = new SQLiteCommand("SELECT * FROM mytable", connection); SQLiteDataReader reader = command.ExecuteReader(); // 读取数据 while (reader.Read()) { string column1 = reader.GetString(0); int column2 = reader.GetInt32(1); // ... } // 关闭数据读取器和连接 reader.Close(); connection.Close(); ``` 在连接串中,Data Source指定SQLite数据库文件的路径,Version指定SQLite的版本号。创建SQLite连接对象后,通过执行SQL语句来操作数据库。使用SQLiteDataReader对象读取数据时,可以通过GetString、GetInt32等方法获取指定列的数据。最后,关闭SQLiteDataReader和连接对象。 ### 回答2: c是C语言的一种编程语言。C语言是一种高级的计算机编程语言,由贝尔实验室的Dennis Ritchie在20世纪70年代开发而来。C语言广泛应用于计算机科学和软件开发领域。 C语言被广泛使用是因为它具有简洁、灵活和高效的特点。它是一种结构化的编程语言,提供了丰富的数据类型和控制结构,使得程序员可以更好地组织和控制程序的流程。C语言还提供了丰富的操作符和库函数,使得编写复杂的算法和数据结构变得更加容易。 C语言还具有可移植性的特点,可以在不同的计算机平台上运行。C语言的程序可以通过简单的修改和重新编译就可以在不同的操作系统和硬件上运行,这使得C语言成为开发跨平台软件的理想选择。 C语言也被广泛应用于系统开发和底层编程。许多操作系统、编译器和数据库系统等底层软件都是使用C语言编写的。C语言的底层编程特性使得程序员可以直接访问和控制计算机的硬件资源,提高了程序的性能和效率。 总之,C语言是一种强大而灵活的编程语言,具有简洁、高效和可移植的特点。它在计算机科学和软件开发领域中得到广泛应用,是学习和掌握计算机编程的重要一步。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值