Set password for SQLite v3 database - vb.net

My application uses a database stored in a file available via network. So far, I've been using a MS-Access file (.accdb), but I'm trying to migrate to SQLite Version 3 (.db3).
I added SQLite NuGet package to my project and created a SQLite database using SQLiteStudio. I refactored my database objects to work with System.Data.SQLite.SQLiteConnection instead of System.Data.OleDb.OleDbConnection and it worked well.
However, my previous accdb database was password protected, and I don't know how to apply a password over my current SQLite database.
Can anyone teach me ho to do it? Thanks in advance!

I followed the link which Wudge kindly appointed in comment above, and it works, but I'd rather clarify what need to be done:
To set a password to an unprotected database:
Dim conn = New SQLite.SQLiteConnection(
"Data Source=C:\yourFolder\yourDB.db3;Version=3;")
conn.Open()
conn.ChangePassword("password")
conn.Close()
To open a password-protected database:
Dim conn = New SQLite.SQLiteConnection(
"Data Source=C:\yourFolder\yourDB.db3;Version=3;")
conn.SetPassword("password")
conn.Open()
conn.Close()
or
Dim conn = New SQLite.SQLiteConnection(
"Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
conn.Open()
conn.Close()
To remove password from a password-protected database:
Dim conn = New SQLite.SQLiteConnection(
"Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
conn.Open()
conn.ChangePassword(String.Empty)
conn.Close()
PS. The open source database manager SQLiteStudio is able to open files which were password-protected that way, as long as you choose System.Data.SQLite instead of Sqlite 3 as your database type. (Requires v 3.1.1, the current version).

Related

Connect MS Access Database (.mdb) using ip address with VB.NET Application

I am connecting my VB.NET windows application to server. I am using this code:
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\162.222.225.78\httpdocs\CM.mdb;"
con.Open()
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
But I am getting error:
'\162.222.225.78\httpdocs\version.txt' is not a valid path. Make sure
that the path name is spelled correctly and that you are connected to
the server on which the file resides.
Please help.
this is the issue for mdw file which is your OLEDB is not finding.
search the .mdw file and copy it in that folder where your mdb file is present.
if it not works do the below..
add Properties("Jet OLEDB:Database Password") = "password" to your connection string
like this..
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=162.222.225.78/CRM.mdb;User=corpopef;Password=**;Properties("Jet OLEDB:Database Password") = "password(try the admin password here)""
if it also not works
write the connection string as "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=162.222.225.78/CRM.mdb;persist security info=false"
have you tried to find connection from the container's(from tool box) properties on page??
Please do two things..
1- take a container control(like datagrid) on your page choose datasource from gridview task,
it will connect to your database where ever it is then copy that connection string from there.
definitly it will work.
2-if it does'nt work plz restart your system as the MS access databse shows this type of problems.

Setting Up SqlConnection string in vb.net for a local database created through visual studio 2010

I have been searching for a couple hours, and found several questions, but none of them really explained this in a way I can understand.
I'm programming a game similar to Rock Paper Sissors, except with many more selections, and the possiblity of a tie. I had originally hardcoded all of the possible outcomes, then decided to try a database so I can learn and practice sql as well.
Problem is, I can't figure out for the life of me how to connect to my local database, now that I have set it up and filled it through Visual Studio 2010. I can connect to it through Server Explorer just fine, and I can see it in Solution Explorer. I've tried several things, along the lines of:
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS; Integrated Security=true")
Dim sqlConn As SqlConnection
sqlConn = New SqlConnection("DataSource=..\..\Outcomes.sdf")
I am relatively new to sql, but know enough through tinkering to build a sql statement and get me the result I want. But I've never connected to a database before. I've looked on MSDN and tried several things I saw on there (everything that looked like what I needed, really) but it still hasn't worked.
If I can connect, I already have my statement set, and have tested it through the database itself. Any help would be wonderful, especially if it's explained in a way I can understand it and use it for later.
Also, if it helps and isn't noticed through my tried code, my db name is Outcomes. I don't know if that is needed or will help, but just in case.
Please visit connection strings here...
It also would have been helpful to know what type of DBMS you are using as well. I noticed you have an .sdf database file, not a DBMS (For ex: MySql, SQL or Oracle). Therefore, your connection string is really going to depend on what you want.
Your issue is here by the way...
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
*You cant use the SqlConnection you have because its not supported with the use of .sdf files.
Instead you have to use: System.Data.SqlServerCe 'This is for compact edition
If you would like to know more about this please see here.
Kendra,
Here are the logical Steps you will need to follow to access the database programmatically:
Note: I'm assumming you have the proper SQLExpress | SQL Server Database setup whether local or remote the methods below are identical except for the connection string information.
1) Import the Sql AdoNet Namespace so you can use the proper SQL Server Client Objects & Methods;
a) Imports System.Data.SqlClient
2) Establish a Connection to the database with the ADO Connection Object:
' Create your ADO Connection Object:
Private myConn As SqlConnection
myConn = New SqlConnection("Initial Catalog=OutComes;" & _
"Data Source=localhost;Integrated Security=SSPI;")
Note: This connection string uses integrated security from your windows machine. you could also use standard security where you would need to enter your username and password credentials. Its your choice.
3) Setup Your ADO Command Object to Define your data retrieval query:
'Create a Command object.
Private myCmd As SqlCommand
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT FirstName, LastName FROM Employees"
'Open the connection.
myConn.Open()
Note: Subsitute CommandText string for your actual query based upon your own database schema.
4) Read, Fetch, Display Data using the SQLDataReader Object:
Private results As String
Private myReader As SqlDataReader
myReader = myCmd.ExecuteReader()
'Traverse the DataSet and Display in GUi for Example:
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab & _
myReader.GetString(1) & vbLf
Loop
'Display results.
MsgBox(results)
5) Gracefully Close all Objects Used:
' Close the reader and the database connection.
myReader.Close()
myConn.Close()
Note - You'll need to consult microsoft for further connection string formats, since I don't have enough info. But this should clarify the actual big picture steps for you.
Regards,
Scott

How to query the local database when using Microsoft sync framework offline?

I have an application built on Microsoft's Sync Framework. Data is entered and stored in database configured with the sync framework wizard. From what I understand, this puts a clone of the server database in a local directory and the database version is SQL Compact edition.
The user will on occasion use the application offline and will need information from one database in order to fill another. They will select a tool from the Tools database and a device with certain parameters from the device database. In order to do this I need to be able to query the local database when offline.
First I attempted to fill my comboboxes by using the local ConnectionString from my settings.
Dim conn As New SqlConnection(My.Settings.ClientDatabaseConnectionString)
conn.Open()
Dim dat As SqlDataAdapter = New SqlDataAdapter("SELECT Distinct InstrumentType FROM CreateScriptTable", conn)
Dim dt As New DataSet
dat.Fill(dt, "TestScriptTable")
cbInst.DataSource = dt.Tables("TestScriptTable").DefaultView
cbInst.DisplayMember = "InstrumentType"
cbInst.ValueMember = "InstrumentType"
Next as it is an SQL ce datbase I used the SQLCeConnection:
Dim conn As SqlCeConnection
conn = New SqlCeConnection("Data Source=|DataDirectory|\TestsDatabase4.sdf;Max Database Size=2047")
Dim dat As SqlCeDataAdapter = New SqlCeDataAdapter("SELECT Distinct InstrumentType FROM TestScriptTable", conn)
Dim dt As New DataSet
dat.Fill(dt, "TestScriptTable")
cbInst.DataSource = dt.Tables("TestScriptTable").DefaultView
cbInst.DisplayMember = "InstrumentType"
cbInst.ValueMember = "InstrumentType"
Various formats for the local connection string were passed into SQLConnection() including removing Max Database Size=2047
Neither of these approaches worked so I tried to change the connection string at runtime following the tutorial here
This did not work either so at the moment I am unable to retrieve any data at will offline.
Has anyone any thoughts on this? Thanks.

Can't open SQLCompact database

I am working with a SQL compact database and I am able to test the connection when I import it and copy the exact file path to my code, but it still says that it hasn't been opened. What am I doing wrong? Are there any shortcuts available if I have already added the database to my project?
Thanks!
Imports System.Data.SqlServerCe
Module Module1
Sub Main()
Dim constring As String = "Data Source=C:\Users\test\Desktop\MyDatabase1.sdf"
Dim conn As New SqlCeConnection(constring)
Dim cmd As New SqlCeCommand("SELECT * FROM ACCOUNT")
conn.Open()
Dim reader As SqlCeDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine(reader)
End While
End Sub
End Module
You need to assign the connection to the command:
Immediately after this line:
conn.Open()
add:
cmd.Connection = conn
Alternatively, you can add the connection to the command's constructor:
Dim cmd As New SqlCeCommand("SELECT * FROM ACCOUNT", conn)
One of the main advantage of using SQL server CE (3.5) is to have Linq to SQL. You should make use of strongly typed database, and of DataContext. if you do so, then creating a new DataContext is ONE line of code, not 8. And if your DataBase file does not exist, the CreateDataTable method of your DataContext will create them for you. Dig a little into this, because using SQL Server CE like an old fashion OLEDB data provider is... well... not optimal :-)

ADO.Net Synchronization & SQL CE

I've succesfully synchronized both source and local db using the local database cache item in VS 2008.
However, I need to access the SQL CE db directly from within another dll/process, and without using a dataset. The reason being that my business object code does not use datasets.
The final code wouldlook something like this:
Dim conn As New SqlServerCe.SqlCeConnection("Data Source=C:\Development\UserDirectory\UserDirectory.DBSyncher\ProfDir.sdf;Persist Security Info=False;")
Dim cmd As New SqlServerCe.SqlCeCommand("Select EmailAddress from Employees Where ID=23", conn)
Dim returnString As String = ""
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
returnString = cmd.ExecuteScalar
conn.Close()
cmd = Nothing
I notice something very strange using a dataset the synchronized changes are shown but accessing the CE database file directly returns old data - no synched data whatsoever.
What am I missing? Any help would be greatly appreciated.
Figured it out!
Forgot that CE is in process, thus it copies the database file(.sdf) to the Debug folder. You have to to reference that database not the one in your project. DOH!