2016-04-14 9 views
1

私はしばらくの間、私の頭を叩いています。私は素晴らしいコーダーではありませんが、私には欠けている簡単な解決策があると思います。私はSystem.Data.SQLiteデータプロバイダを使用して暗号化されたSQLite dbを作成するためにいくつかのPoCコードに取り組んできました。私はC#で基本的なコンソールアプリケーションを書いています。ここに私の問題がある。暗号化されたsqliteデータベースをC言語で読むには#

データベースを暗号化する接続文字列にパスワードを付けて新しいsqliteデータベースを作成することができます。

try 
{ 
    if (!System.IO.File.Exists(@"c:\temp\test.db.sqlite")) 
    { 
     System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\temp\test.db.sqlite"); 
    } 

    System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=c:\temp\test.db.sqlite;Version=3;password=abc"); 
    conn.Open(); 

    System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(conn); 
    //System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("create table test (name char(50))", conn); 
    //cmd.ExecuteNonQuery(); 


    cmd.CommandText = "insert into test values ('my string')"; 
    cmd.ExecuteNonQuery(); 

    cmd.CommandText = "select * from test"; 

    var dr = cmd.ExecuteReader(); 
    while (dr.Read()) 
    { 
     Console.WriteLine(dr.GetString(0)); 
    } 

    conn.Close(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e.ToString()); 
    throw; 
} 

これはうまくいくようです...テーブルを作成し、テーブルにデータを追加してからテーブルをクエリできます。表を再作成しないように、私はそれを実行してみてください次回は、私はそうでない場合、コードは同じでなければなりません...以下の変更を行います:

... 

if (!System.IO.File.Exists(@"c:\temp\test.db.sqlite")) 
{ 
    System.Data.SQLite.SQLiteConnection.CreateFile(@"c:\temp\test.db.sqlite"); 
} 

System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(@"Data Source=c:\temp\test.db.sqlite;Version=3;password=abc"); 
conn.Open(); 

//System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(conn); 
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("create table test (name char(50))", conn); 
cmd.ExecuteNonQuery(); 


cmd.CommandText = "insert into test values ('my string')"; 

... 

私はそれをもう一度実行すると、I次のエラーが表示されます。

System.Data.SQLite.SQLiteException (0x80004005): file is encrypted or is not a database file is encrypted or is not a database 
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain) 
at System.Data.SQLite.SQLiteCommand.BuildNextCommand() 
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) 
at System.Data.SQLite.SQLiteDataReader.NextResult() 
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) 
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) 
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior) 

私は間違っていると思いますか?作成して暗号化したdbを再オープンして使用するにはどうすればよいですか?

+1

'SQLiteConnection.CreateFile'を使う必要はないと思います。存在しないファイルに接続すると、デフォルトで作成されます。その通話を削除してみてください。おそらく、暗号化を妨害しているかもしれません。(ちょっと推測すると)。 – Blorgbeard

答えて

0

パスワードの設定:

string conn = @"Data Source=database.s3db;Password=Mypass;"; 
SQLiteConnection connection= new SQLiteConnection(conn); 
connection.Open(); 
//Some code 
connection.ChangePassword("Mypass"); 
connection.Close(); 

パスワード変更:パスワードを使用して

string conn = @"Data Source=database.s3db;"; 
SQLiteConnection connection= new SQLiteConnection(conn); 
connection.Open(); 
//Some code 
connection.ChangePassword("Mypass"); 
connection.Close(); 

接続は、接続文字列から行うことができます。

string conn = @"Data Source=database.s3db;Password=Mypass;"; 

か:

string conn = @"Data Source=database.s3db;"; 
SQLiteConnection connection= new SQLiteConnection(conn); 
connection.Open(); 
//Some code 
connection.SetPassword("Mypass"); 
connection.Close(); 
関連する問題