I write code with c#.
I have a encrypted sqlite database. I created this database in SQLite Maestro. on this program, right click, manage encryption, I give password which I want to. This file has only database tables, so it has no data. I added this file in my code as Embedded Resource. But, I try reading this database file and take error.
Error : file is not database
I use Entity Framework.
What I have tried:
//create encrypted sqlite database file with new name
internal static void CreateDatabase(string fullName)
{
CodeFirst = true;
DataSource = fullName;
Assembly asm = Assembly.GetExecutingAssembly();
string ns = typeof(Context).Namespace;
if (File.Exists(fullName))
{
File.Delete(fullName);//Backup file will delete
}
using (Stream stream = asm.GetManifestResourceStream($"{ns}.sqlitedatabasefile"))
{
using (FileStream fileStream = new FileStream(fullName, FileMode.CreateNew, FileAccess.Write))
{
stream.CopyTo(fileStream);
}
}
}
//Then, I write below codes to control of version and to connnect database
public Context() : base(new SQLiteConnection
{
ConnectionString = new SQLiteConnectionStringBuilder
{
DataSource = DataSource,
ForeignKeys = true,
BinaryGUID = false,
Pooling = false,
SetDefaults = false,
PageSize = 1024,
Version = 3,
//Password= "1234" //not working
}.ConnectionString
}, true)
{
DbConfiguration.SetConfiguration(new SQLiteConfiguration());
if (Database.Connection is SQLiteConnection conn)
{
//conn.setPassword("123"); //not working
conn.Open();
using (SQLiteCommand cmd = conn.CreateCommand())
{
bool hasColumn= false;
cmd.CommandText = "PRAGMA table_info(Student)"; //Student is table.
using (SQLiteDataReader reader = cmd.ExecuteReader())//ERROR.
{
int nameIndex = reader.GetOrdinal("Name");
while (reader.Read())
{
if (reader.GetString(nameIndex).Equals("Age"))
{
hasColumn= true;
break;
}
}
}
//Add Age column in Student table
if (!hasColumn)
{
Database.ExecuteSqlCommand("ALTER TABLE Student ADD COLUMN Age integer DEFAULT -1");
//Database.ExecuteSqlCommand($@"UPDATE Setting SET Version= 2 , ReleaseDate ='{DateTime.Now.ToShortDateString()}'");
var version = Setting.FirstOrDefaultAsync();
version.Result.Version = 2;
version.Result.ReleaseDate = DateTime.Now;
SaveChanges();
System.Windows.Forms.MessageBox.Show("Your database updated...", "Information Message", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
}
}