Sqlite3内存数据库
Sqlite3内存数据库
Sqlite3是一个小型的,嵌入式的数据库。它还具有一个特色的功能:即内存数据库模式(与Redis ,Memcached等不同)。 这篇文章使用 .Net 5.0 平台演示实现Sqlite3的内存数据库模式。
环境及依赖
我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
- 开发环境**.Net 5.0**
- VS2019
- Nuget 包: Microsoft.Data.Sqlite
功能演示-内存模式
using Microsoft.Data.Sqlite;
public class MsSqliteTest
{
string MemoryConnStr = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";
string FileConnStr = "Data Source=MsSqliteTest.db;Cache=Shared";
SqliteConnection Conn;
public bool CheckTableExists(SqliteConnection Conn, string tableName)
{
var comm = Conn.CreateCommand();
comm.CommandText = $"Select Count(name) FROM sqlite_master WHERE type='table' AND name='{tableName}';";
int rt = System.Convert.ToInt32(comm.ExecuteScalar());
return rt > 0;
}
public bool OpenCreateTable(SqliteConnection Conn)
{
Conn.Open();
if (CheckTableExists(Conn, "Users") == false)
{
var comm = Conn.CreateCommand();
comm.CommandText = "Create Table Users (Id Integer PRIMARY KEY AUTOINCREMENT, Name CHAR (2, 20) NOT NULL, Age INTEGER);";
int rt = System.Convert.ToInt32(comm.ExecuteNonQuery());
comm.Dispose();
return rt > 0;
}
return true;
}
public int InsertTable(SqliteConnection Conn)
{
var comm = Conn.CreateCommand();
comm.CommandText = "insert into Users (Name,Age) Values ('xiaoming1',25)";
int rt = System

最低0.47元/天 解锁文章
1866

被折叠的 条评论
为什么被折叠?



