1. Recently studied the use of SQLite as a DB file simple encryption and decryption
1 Private Staticsqliteconnection getconnection ()2 {3 Sqliteconnection Conn;4 stringPassword = Dataprovider.configsearch (Configcode.sys_file_encryption_password,true)[0]. Value;5 stringDBFile =Path.Combine (Environment.getfolderpath (Environment.SpecialFolder.CommonApplicationData), Configcode.client_ Data_path);6 stringDbPath =Path.getdirectoryname (dbfile);7 if(!directory.exists (dbPath))8 {9 directory.createdirectory (dbPath);Ten } One if(!file.exists (dbfile)) A { - //database does not exist, create - Sqliteconnection.createfile (dbfile); the commonutils.setaccesscontrollist (dbfile); -conn =NewSqliteconnection (string. Format ("Data source={0}; Pooling=true; Failifmissing=false", DBFile)); - Conn. Open (); - Conn. ChangePassword (password); + if(string. IsNullOrEmpty (install_db_sql)) - { +Install_db_sql = Path.Combine (Path.getdirectoryname (typeof(Sqliteprovider). Assembly.location),"Setup.sql"); A } at - foreach(stringLineinchFile.readalltext (Install_db_sql). Split (New Char[] {';' })) - { - if(String.IsNullOrEmpty (line)) - { - Continue; in } -Sqlitecommand cmd =Conn. CreateCommand (); toCmd.commandtext =Line ; +Cmd.commandtype =CommandType.Text; - cmd. ExecuteNonQuery (); the cmd. Dispose (); * } $ }Panax Notoginseng Else - { the Try + { Aconn =NewSqliteconnection (string. Format ("Data source={0}; Pooling=true; Failifmissing=false", DBFile)); the Conn. SetPassword (password); + Conn. Open (); - Conn. ChangePassword (password); $ } $ Catch(Exception) - { -conn =NewSqliteconnection (string. Format ("Data source={0}; Pooling=true; Failifmissing=false", DBFile)); the Conn. Open (); - }Wuyi the } - returnConn; Wu}
View Code
Here in order to take into account the previous unencrypted database files, deliberately do the processing, if the database does not exist, directly created, encryption does not exist this problem, mainly for the existing database has a Try...catch exception handling, the individual has been verified, can be used directly.
C # leverages SQLite to create databases for. DB and. LOGDB encryption and decryption and SQLite