解密sqlite数据库中的值并在datagrid上查看它们

时间:2018-09-06 17:33:39

标签: c# wpf sqlite

wpf的新手,在如何解密数据库中的加密值以查看datagrid时遇到了一些麻烦,我一直在寻找解决方案将近2天,只想出了这段代码< / p>

//private static String Encrypt(string Text)
//private static String Decrypt(string Text)
private void refresh_datagrid()
{

//create temp directory

var tempdir = Path.GetFullPath(Path.Combine(Path.GetTempPath(),Guid.NewGuid().ToString()));
Directory.CreateDirectory(tempdir);

//create temp db with same table and details same as on original db

        SQLiteConnection.CreateFile(tempdir+"tempdb.sqlite;security=true;");
        var con1 = new SQLiteConnection("Data 
        Source="+tempdir+"\\tempdb.sqlite;");
        con1.Open();
        con1.ChangePassword(defaultpassword);
        String sql = "CREATE TABLE acc (nam STRING, pas STRING)";
        SQLiteCommand command = new SQLiteCommand(sql, con1);
        command.ExecuteNonQuery();

//open a connection to encrypted db

        using (var con2 = new SQLiteConnection("DataSource="+db_path+";security=true;"))
        { 
            con2.Open();
            con2.ChangePassword(defaultpassword);

            SQLiteCommand cmd = con2.CreateCommand();

            cmd.CommandText = "SELECT * FROM acc ";

//read encrypted strings to decrypt it then pass them to tempdb

            SQLiteDataReader reader;
            reader = cmd.ExecuteReader();
           while(reader.Read())
            {
                SQLiteCommand cmdc = con1.CreateCommand();
                cmdc.CommandText = "INSERT INTO  acc (nam,pas) VALUES (@username ,@password )";

                cmdc.Parameters.AddWithValue("@username", Decrypt(reader["nam"].ToString()));
                cmdc.Parameters.AddWithValue("@password", Decrypt(reader["pas"].ToString()));

                cmdc.ExecuteNonQuery();
            }

//view tempdb data on datagrid

            SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(cmd.CommandText, con1);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            control.ItemsSource = dataTable.AsDataView();

            con1.Close();
             con2.Close();

//remove tempdir and tempdb 

            Directory.Delete(tempdir,true);
        }
}

它可以工作,但这只是一个简化的代码。 在我的完整代码上,我有6个更多详细信息和120个帐户的更多表格 当我尝试在我的数据网格上查看它们时,应用程序挂起3-4秒,我遇到了backgroundworker这个问题 但仍对延迟不满意,我需要一种更好的方法来处理此问题,对此表示感谢。

ps:对任何错误的语法或EN并非我的母语拼写感到抱歉

1 个答案:

答案 0 :(得分:0)

  1. 尝试在两行之间添加日志以找出瓶颈
  2. 首先将所有记录从加密的数据库读取到内存中,而不是在读取时写入
  3. 数据库中有多少行?您是否将所有6个表都联接到查询中?考虑sql调优