对数据加密分两种,一种是对数据库本身进行加密,另一种是对数据表中的数据进行加密,

如果SQLite数据库加密,我这里使用的一个管理工具叫SQLiteDeveloper,如下就可以加密数据库

如果在工具中不提供密码的情况下打开数据库,会给你错误提示如下:

或者在C# 使用错误的密码也会给你错误提示:

System.Data.SQLite.SQLiteException:“file is encrypted or is not a database

 正确的连接方式就是在连接字符串中提供正确的密码:

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenSqliteDBByPwd
{
    class Program
    {
        static void Main(string[] args)
        {

            string DB_PATH = "Data Source=EncryptedDB.db3; Password=1111";


            using (SQLiteConnection con = new SQLiteConnection(DB_PATH))
            {
                con.Open();
                string sqlStr = @"INSERT INTO Customer(CUST_NO,CUSTOMER)
                                  VALUES
                                  (
                                      3001,
                                      'Allen'
                                  )";
                using (SQLiteCommand cmd = new SQLiteCommand(sqlStr, con))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!