SQLite.Net使用入门(一)

前端之家收集整理的这篇文章主要介绍了SQLite.Net使用入门(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

逆境是成长必经的过程,能勇于接受逆境的人,生命就会日渐的茁壮。


Program.cs主程序:

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();
        }

        //创建一个两列的名为“highscores” 表:name(最多20个字符的字符串) score(一个int)
        void createTable()
        {
            string sql = "create table highscores (name varchar(20),score int)";
            sqliteCommand command = new sqliteCommand(sql,m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入一些值到highscores表中
        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,score) values ('And I',9001)";
            command = new sqliteCommand(sql,m_dbConnection);
            command.ExecuteNonQuery();
        }

        //按降序排序 高分
        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();
        }
    }

运行结果如图:

原文链接:https://www.f2er.com/sqlite/198716.html

猜你在找的Sqlite相关文章