Connection with SQLite db encrypted

I'm trying to create a program that communicates with a SQLite database encrypted by a password. If I try with a db without any password it work, but with a database with password, after connecting, when executing the query it gives the error "File is not a database". This is the code:

using Microsoft.Data.Sqlite;
using System;
using System.Data.SQLite;

...

string conn = "Data Source=C:\\Users\\Utente\\Desktop\\SQLite\\sqlite tools\\DB\\provapassword.db;";
SqliteConnection sql_conn = new SqliteConnection(conn);
sql_conn.Open();
Console.WriteLine("Connected to provapassword");
var cmd = sql_conn.CreateCommand();
cmd.CommandText = "SELECT quote($password);";
cmd.Parameters.AddWithValue("$password", "prova");
var quotedPassword = (string)cmd.ExecuteScalar();
cmd.CommandText = "PRAGMA key = " + quotedPassword;
cmd.Parameters.Clear();
cmd.ExecuteNonQuery();                  
try
{     
//INSERT
string query = "INSERT INTO nomi values(10, \"AAA\")";
cmd.CommandText = query;
if (cmd.ExecuteNonQuery() != 0)
    Console.WriteLine("OK");
else
    Console.WriteLine("Error");
sql_conn.Close();
Console.WriteLine("Connection close"); 
}
catch (Exception e)
{
   Console.WriteLine(e.Message.ToString());
}

Do you have any advice? Thanks

0 answers