C#操作SQLite数据库

简介:

SQLite介绍

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。

SQLite数据库官方主页:http://www.sqlite.org/index.html

C#操作SQLite Database

C#下SQLite操作驱动dll下载:System.Data.SQLite

C#使用SQLite步骤:

(1)新建一个project

(2)添加SQLite操作驱动dll引用

(3)使用API操作SQLite DataBase

复制代码
using System;
using System.Data.SQLite;

namespace SQLiteSamples
{
    class Program
    {
        //数据库连接
        SQLiteConnection m_dbConnection;

        static void Main(string[] args)
        {
            Program p = new Program();
        }

        public Program()
        {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            printHighscores();
        }

        //创建一个空的数据库
        void createNewDatabase()
        {
            SQLiteConnection.CreateFile("MyDatabase.sqlite");
        }

        //创建一个连接到指定数据库
        void connectToDatabase()
        {
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
            m_dbConnection.Open();
        }

        //在指定数据库中创建一个table
        void createTable()
        {
            string sql = "create table highscores (name varchar(20), score int)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入一些数据
        void fillTable()
        {
            string sql = "insert into highscores (name, score) values ('Me', 3000)";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('Myself', 6000)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('And I', 9001)";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //使用sql查询语句,并显示结果
        void printHighscores()
        {
            string sql = "select * from highscores order by score desc";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
                Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
            Console.ReadLine();
        }
    }
}
复制代码

关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/

SQLite GUI客户端列表:http://www.sqlite.org/cvstrac/wiki?p=ManagementTools

SQLite Administrator下载地址:http://download.orbmu2k.de/files/sqliteadmin.zip

操作SQLite Database的C#帮助类SQLite Helper

将一些常用的功能封装一下,封装成SQLite Helper类

  View Code

Codeproject上的一个封装:http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp

 


    本文转自阿凡卢博客园博客,原文链接:http://www.cnblogs.com/luxiaoxun/p/3784729.html,如需转载请自行联系原作者



相关文章
|
分布式计算 DataWorks 调度
oss数据同步maxcompute报错
在使用阿里云DataWorks同步OSS数据至MaxCompute时,遇到“Input is not in the .gz format”的报错。问题源于目标目录中存在一个空文件,导致同步时识别错误。
|
XML Java 测试技术
从零开始学 Maven:简化 Java 项目的构建与管理
Maven 是一个由 Apache 软件基金会开发的项目管理和构建自动化工具。它主要用在 Java 项目中,但也可以用于其他类型的项目。
583 1
从零开始学 Maven:简化 Java 项目的构建与管理
|
存储 传感器 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK修改图像像素格式Mono8或者Mono10(C++)
401 0
ThreeJs模拟工厂生产过程七
这篇文章详细介绍了如何在Three.js中为工厂车间的货架动态生成并放置货物,通过循环逻辑和贴图应用使货架上的物品更加逼真,增强了场景的真实感。
177 0
|
NoSQL Java Redis
|
Android开发
error: GLES2/gl2.h: No such file or directory
error: GLES2/gl2.h: No such file or directory
|
并行计算 Shell Docker
【环境配置】Ubuntu16.04安装nvidia-docker
【环境配置】Ubuntu16.04安装nvidia-docker
484 2
|
机器学习/深度学习 算法 数据挖掘
深度学习500问——Chapter05: 卷积神经网络(CNN)(4)
深度学习500问——Chapter05: 卷积神经网络(CNN)(4)
200 1
|
关系型数据库 MySQL Linux
DolphinScheduler2.x 伪分布式部署
DolphinScheduler2.x 伪分布式部署
427 0
|
机器学习/深度学习 编解码 自然语言处理
ICCV 2023 | SwiftFormer:基于Transformer的实时移动视觉应用中的高效加性注意
ICCV 2023 | SwiftFormer:基于Transformer的实时移动视觉应用中的高效加性注意
321 2