[SQLite入门教程]Visual Studio下SQLite配置使用

Visual Studio下SQLite配置使用

1.工具->NuGet包管理器->管理解决方案的NuGet程序包

  搜索System.Data.SQLite进行安装(只安装这一个包,其他的不安装),安装成功后项目引用中出现System.Data.SQLite、System.Data.SQLite.Linq两个引用包

二、验证搭建环境是否正常:

 IDE环境:Visual Studio 2022 Professional 17.8.6
测试程序框架:.NET Framework 3.0
SQLite版本号:1.0.119.0

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Text;

namespace SqliteTest
{
    public class Program
    {
        public static string FilePath = @".\cfg.db";

        public static void Main(string[] args)
        {
            SQLiteConnection.CreateFile(FilePath);
            SQLiteConnection conn = new SQLiteConnection($"Data Source={FilePath};Version=3");
            conn.Open();
            string sql1 = "create table AllPerson(" +
                "id integer primary key autoincrement," +
                "name text not null," +
                "cardId text not null," +
                "phoneId text not null)";
            SQLiteCommand cmd = new SQLiteCommand(sql1, conn);
            cmd.ExecuteNonQuery();

            string sql2 = $"insert into AllPerson(name,cardId,phoneId) " +
                $"values('方羽','620821198708110319','13993303115')";
            SQLiteCommand cmd2 = new SQLiteCommand(sql2, conn);
            cmd2.ExecuteNonQuery();

            string sql3 = $"select * from AllPerson";
            SQLiteCommand cmd3 = new SQLiteCommand(sql3, conn);
            SQLiteDataReader reader = cmd3.ExecuteReader();
            
            while(reader.Read())
            {
                Console.WriteLine($"Id:{reader[0]}\n" +
                    $"name:{reader[1]}\n" +
                    $"cardId:{reader[2]}\n" +
                    $"phoneId:{reader[3]}");
            }

            reader.Close();
            conn.Close();
            Console.ReadKey(true);
        }
    }
}

运行输出:

 

posted @ 2019-12-25 18:38  修道者~  阅读(2612)  评论(0)    收藏  举报