IT评测·应用市场-qidao123.com

标题: SqLite本地数据库常规用法 [打印本页]

作者: 九天猎人    时间: 2022-8-9 16:46
标题: SqLite本地数据库常规用法
SqLite数据库介绍

SqLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置。
就像其他数据库,SqLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SqLite 直接访问其存储文件。
为什么要用SqLite

创建SqLite数据库

使用命令行创建数据库
  1. cd. >test3.db
复制代码
使用代码创建数据库
  1. //方式一
  2. System.IO.File.Create(@"c:\test.db");
  3. //方式二
  4. //需要安装Neget包。.Net FX安装 System.Data.SQLite;.Net Core安装 System.Data.SQLite.Core
  5. System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\test2.db");
复制代码
使用官网工具创建数据库

下载SqLite工具包
博客下载地址
官网下载地址

创建数据库
工具包下载完成后j解压,可以看到SqLite3.exe文件,直接在命令行当前目录中以下命令创建数据库。
  1. sqlite3 "test.db" ".databases" ".quit"
复制代码

连接数据库

test.db文件创建完成以后,默认会创建一个main名称的数据库,然后就可以直接用数据库IDE打开连接数据库即可。
数据库IDE:
代码操作数据库

链接字符串

实体类操作Sqlite数据库

需要Neget安装sqlite-net-pcl包。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using SQLite;
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string path = @"F:\DownLoad\sqlite\text4.db";
  12.             NoteDatabase noteDatabase = new NoteDatabase(path);
  13.             Note note = new Note();
  14.             note.Text = "123456";
  15.             note.Date = DateTime.Now;
  16.             noteDatabase.SaveNoteAsync(note).Wait();
  17.         }
  18.     }
  19.     public class Note
  20.     {
  21.         //主键,自增
  22.         [PrimaryKey, AutoIncrement]
  23.         public int ID { get; set; }
  24.         //唯一标识,不为空
  25.         [Unique, NotNull]
  26.         public string Text { get; set; }
  27.         [MaxLength(30), NotNull]
  28.         public DateTime Date { get; set; }
  29.     }
  30.     public class NoteDatabase
  31.     {
  32.         readonly SQLiteAsyncConnection database;
  33.         readonly SQLite.SQLiteConnection database2;
  34.         public NoteDatabase(string connectionString)
  35.         {
  36.             database = new SQLiteAsyncConnection(connectionString);
  37.             database.CreateTableAsync<Note>().Wait();
  38.         }
  39.         public Task<List<Note>> GetNotesAsync()
  40.         {
  41.             //Get all notes.
  42.             return database.Table<Note>().ToListAsync();
  43.         }
  44.         public Task<Note> GetNoteAsync(int id)
  45.         {
  46.             // Get a specific note.
  47.             return database.Table<Note>()
  48.                             .Where(i => i.ID == id)
  49.                             .FirstOrDefaultAsync();
  50.         }
  51.         public Task<int> SaveNoteAsync(Note note)
  52.         {
  53.             if (note.ID != 0)
  54.             {
  55.                 // Update an existing note.
  56.                 return database.UpdateAsync(note);
  57.             }
  58.             else
  59.             {
  60.                 // Save a new note.
  61.                 return database.InsertAsync(note);
  62.             }
  63.         }
  64.         public Task<int> DeleteNoteAsync(Note note)
  65.         {
  66.             // Delete a note.
  67.             return database.DeleteAsync(note);
  68.         }
  69.     }
  70. }
复制代码
ADO.NET

需要安装Neget包。.Net FX安装 System.Data.SQLite;.Net Core安装System.Data.SQLite.Core。
  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Data.SQLite;
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             SqlHelper helper = new SqlHelper();
  12.             string strSql = "Select * From Test";
  13.             DataTable dt = helper.GetDataTable(strSql);
  14.             string updateSql = "update Test set id=4 where id=3";
  15.             int count = helper.ExecuteNonQuery(updateSql);
  16.             Console.WriteLine(count);
  17.         }
  18.     }
  19.     public class SqlHelper
  20.     {
  21.         //创建数据库连接字符串
  22.         private static string ConnString = @"Data Source=F:\DownLoad\sqlite\test.db;database=main;";
  23.         //创建数据库连接对象
  24.         private static SQLiteConnection Conn = null;
  25.         //增删改
  26.         public int ExecuteNonQuery(string sqlStr)
  27.         {
  28.             Conn = new SQLiteConnection(ConnString);
  29.             Conn.Open();
  30.             IDbCommand cmd = new SQLiteCommand(sqlStr, Conn);
  31.             int result = cmd.ExecuteNonQuery();
  32.             Conn.Close();
  33.             return result;
  34.         }
  35.         /// <summary>
  36.         /// 获得datatable
  37.         /// </summary>
  38.         /// <param name="sql"></param>
  39.         /// <returns></returns>
  40.         public DataTable GetDataTable(string sqlStr)
  41.         {
  42.             Conn = new SQLiteConnection(ConnString);
  43.             DataTable dt = new DataTable();
  44.             DbDataAdapter dap = new SQLiteDataAdapter(sqlStr, Conn);
  45.             dap.Fill(dt);
  46.             Conn.Close();
  47.             return dt;
  48.         }
  49.     }
  50. }
复制代码
Dapper

需要Neget安装System.Data.SQLite和Dapper包。
  1. using Dapper;
  2. using System.Data.SQLite;
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             using (var conn = new SQLiteConnection(@"Data Source=F:\DownLoad\sqlite\text4.db;database=main;"))
  10.             {
  11.                 var data = conn.Query<object>("Select * From Test");
  12.                 int a = conn.Execute("update Test set id=4 where id=3");
  13.             }
  14.         }
  15.     }
  16. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4