• SQLLITE HELPER


    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();
            }
        }
    }
  • 相关阅读:
    The analysis of China's holding the Olympic Games
    ASP.NET获取客户端的操作系统、浏览器、.NET版本等信息(图)
    ASP.NET 无提示关闭窗口
    VC 使用CryptoAPI计算Hash值:MD5, SHA
    SQL Server 2005 中设置某个用户对某一个数据库有完全控制的权限
    C# 计算文件的MD5值
    VC 获取物理网卡的MAC地址
    哪些免费邮箱不在邮件内容里插广告?
    忆同学
    使用正则表达式获取连接字符串某项的值
  • 原文地址:https://www.cnblogs.com/qiu18359243869/p/10759496.html
Copyright © 2020-2023  润新知