Nuget 安装以下程序包

1.SqlSugarCore

2.Microsoft.Data.Sqlite.Core

3.SQLitePCLRaw.bundle_e_sqlcipher

using Microsoft.Data.Sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTest
{

    /// <summary>
    /// Sqlsugar测试
    /// </summary>
    [TestClass]
    public class SqlsugarTest
    {

        /// <summary>
        /// 测试创建数据库
        /// </summary>
        [TestMethod]
        public void TestCreateSqliteDB()
        {
            try
            {
                //生成连接字符串
                var connectionString = new SqliteConnectionStringBuilder("DataSource=C:/Users/hongz/Desktop/test.db;")
                {
                    Mode = SqliteOpenMode.ReadWrite,
                    Password = "123456"
                }.ToString();

                //使用Sqlsugar连接Sqlite
                using (SqlSugarClient client = new SqlSugarClient(
                    new ConnectionConfig()
                    {
                        ConnectionString = connectionString,
                        IsAutoCloseConnection = true,
                        DbType = DbType.Sqlite,
                    }))
                {
                    client.Open();

                    /**
                     * 单单调用 Open() 是无法判断是否成功打开加密Sqlite数据库的
                     * 在查询数据时,如果密码错误或者未输入密码会报错.反之成功
                     */
                    List<abc> lt = client.Queryable<abc>().ToList();

                    //测试断言
                    Assert.IsTrue(lt.Count == 1);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
                //throw;
            }
        }
    }

    /// <summary>
    /// 临时测试实体表
    /// </summary>
    class abc
    {
        public string a1 { get; set; }
    }
}

本Demo仅展示Sqlsugar连接加密Sqlite部分,Sqlsugar IOC  等请自行查询

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐