c#SQLite数据库连接字符串
更新时间:2023-01-05 作者:
本文介绍如何使用c#链接SQLite
(基本的) Data Source=filename;Version=3; (使⽤编码)Using UTF16 Data Source=filename;Version=3;UseUTF16Encoding=True (带密码的) Data Source=filename;Version=3;Password=myPassword; (使⽤前数据库格式)Using the pre 3.3x database format3.3x Data Source=filename;Version=3;Legacy Format=True; (只读连接) Data Source=filename;Version=3;Read Only=True; (设置连接池) Data Source=filename;Version=3;Pooling=False;Max Pool Size=100; 使用DateTime。刻度作为日期时间格式 Data Source=filename;Version=3;DateTimeFormat=Ticks; (把作为⽂本存储,默认是) Data Source=filename;Version=3;BinaryGUID=False; 如果把Guid作为⽂本存储需要更多的存储空间(指定⼤⼩) Data Source=filename;Version=3;Cache Size=2000; Cache Size 单位是字节(指定页⼤⼩) Data Source=filename;Version=3;Page Size=1024; 指定连接Sqlite数据库的缓存⼤⼩: Data Source=mydb.db;Version=3;Cache Size=3000;
SQLite的连接字符串
--Basic(基本的) Data Source=filename;Version=3; --Using UTF16(使用UTF16编码) Data Source=filename;Version=3;UseUTF16Encoding=True; --With password(带密码的) Data Source=filename;Version=3;Password=myPassword; --Using the pre 3.3x database format(使用3.3x前数据库格式) Data Source=filename;Version=3;Legacy Format=True; --Read only connection(只读连接) Data Source=filename;Version=3;Read Only=True; --With connection pooling(设置连接池) Data Source=filename;Version=3;Pooling=False;Max Pool Size=100; --Using DateTime.Ticks as datetime format() Data Source=filename;Version=3;DateTimeFormat=Ticks; --Store GUID as text(把Guid作为文本存储,默认是Binary) Data Source=filename;Version=3;BinaryGUID=False; -- 如果把Guid作为文本存储需要更多的存储空间 --Specify cache size(指定Cache大小) Data Source=filename;Version=3;Cache Size=2000; -- Cache Size 单位是字节 --Specify page size(指定页大小) Data Source=filename;Version=3;Page Size=1024; -- Page Size 单位是字节
加载加密sqlite到内存
背景
var connectionString = new SqliteConnectionStringBuilder("Data Source=sampleDatabase.db") { Mode = SqliteOpenMode.ReadWriteCreate }.ToString(); var connection = new SqliteConnection(connectionString); connection.Open(); var inMemConnectionString = new SqliteConnectionStringBuilder("Data Source=InMemorySample;Mode=Memory;Cache=Shared") { Mode = SqliteOpenMode.ReadWriteCreate }.ToString(); var inMemConnection = new SqliteConnection( inMemConnectionString ); inMemConnection.Open(); connection.BackupDatabase(inMemConnection, connection.Database, connection.Database);
问题
backup is not supported with encrypted databases.
解决
var connectionString = new SqliteConnectionStringBuilder("Data Source=encrypted.db") { Mode = SqliteOpenMode.ReadWriteCreate, Password = "somePassword" }.ToString(); var connection = new SqliteConnection(connectionString); connection.Open(); var query = "ATTACH DATABASE 'file:InMemorySample?mode=memory&cache=shared' AS inMemDb KEY ''; SELECT sqlcipher_export('inMemDb'); DETACH DATABASE inMemDb;"; using var cmd = new SqliteCommand(query, connection); var res = cmd.ExecuteNonQuery(); connection.Close(); var inMemConnectionString = new SqliteConnectionStringBuilder("Data Source=InMemorySample;Mode=Memory;Cache=Shared") { Mode = SqliteOpenMode.ReadWriteCreate }.ToString(); var inMemConnection = new SqliteConnection( inMemConnectionString ); inMemConnection.Open();