Connecting or creating sqlite database with password in dotnet

When working with Sqlite.net in dotnet programs we can connect or create Sqlite db with or without password. If we will create db with password not one can access your data without password given by you.

Connection string for SQLite db in dotnet is like
String _connString =@”data source=D:bank_data.db;password=password”;

In above connection string there are two parameters

data source, this is path of db file, if you have not given full path then it will make relative path to you application execution path.
  1. password, this is optional field and you have to give this when you want to connect or create database with or without password.

For connecting sqlite db with password, here is one c# example

String _connString =@"data source=D:bank_data.db;password=password";

SQLiteConnection _conn = new SQLiteConnection();
_conn.ConnectionString = _connString;
_conn.Open();

You can create SQLiteConnection object as given above and use this connection for different ado.net operation like filling database, executing commands.
For setting password or adding password late with code SQQLiteConnection object provides two methods(overloaded)

public void SetPassword(byte[] databasePassword)
Summary:
Sets the password for a password-protected database. A password-protected database is unusable for any operation until the password has been set.
Parameters:
databasePassword: The password for the database

public void SetPassword(string databasePassword)
Summary:
Sets the password for a password-protected database. A password-protected database is unusable for any operation until the password has been set.
Parameters:
databasePassword: The password for the database

For changing password or adding password late with code SQQLiteConnection object provides two methods(overloaded)

public void ChangePassword(byte[] newPassword)
Summary:
Change the password (or assign a password) to an open database.
Parameters:
newPassword: The new password to assign to the database
Remarks:
No readers or writers may be active for this process. The database must already be open and if it already was password protected, the existing password must already have been supplied.

public void ChangePassword(string newPassword)
Summary:
Change the password (or assign a password) to an open database.
Parameters:
newPassword: The new password to assign to the database
Remarks:
No readers or writers may be active for this process. The database must already be open and if it already was password protected, the existing password must already have been supplied.

Leave a Reply

Your email address will not be published. Required fields are marked *