Click here to Skip to main content
15,884,598 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}

}
}
}
Posted
Updated 15-Apr-19 22:39pm
Comments
Richard MacCutchan 16-Apr-19 3:46am    
You need to decrypt the file before you write it to disk.
CengBrothers 16-Apr-19 4:42am    
where and how ? I don't understand you. I use SQLite Meastro and Db Browser for SQLite. I give a password in one of the other program does not open, or vice versa, I'm doing in the editors.
Richard MacCutchan 16-Apr-19 6:36am    
No idea, I have no experience of Maestro. I suggest, as below, you use their support forum.

1 solution

 
Share this answer
 
Comments
CengBrothers 17-Apr-19 7:25am    
thks, I try it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900