• C#操作SQLite数据库


    【转】http://www.cnblogs.com/luxiaoxun/p/3784729.html

    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"] + "	Score: " + 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文件

    //数据库名称
    string Tdb = "";
    
    if (File.Exists(@"."+Tdb+".sqlite"))
    {
    MessageBox.Show("存在重名文件 添加失败");
    
    }
    else
    {
    MessageBox.Show("添加成功");
    sqlite.createNewDatabase(Tdb);
    }
  • 相关阅读:
    Android 通过ViewFlipper实现广告轮播功能并可以通过手势滑动进行广告切换
    [C#][DevPress]自定义数据分页控件
    [C#][SQLLITE]一个数据分页技巧
    [C#]用SharpZipLib压缩多个文件
    [C#][DevPress]省市县乡嵌套查询
    [C#][DevPress]事件委托的使用
    [C#]Excel操作类
    [C#][DevPress]手动添加控件中的子控件或者结构
    [C#]XML操作类
    [C#]把DataTable转换成泛型实体List
  • 原文地址:https://www.cnblogs.com/baimangguo/p/7009191.html
Copyright © 2020-2023  润新知