The previous article has introduced the SQLite database and ADO. NET Provider for SQLite. Now we will introduce how to use c # To operate the SQLite database.
1. Download ADO. NET provider for the SQLite database engine from the http://sourceforge.net/projects/sqlite-dotnet2/files/. then install.
2. Create a console program in VS 2005 and add reference System. Data. SQLite. dll. the file is in the bin directory of the ADO. NET provider for the SQLite installation directory.
3. The using namespace can be used like other ADO. NET providers.
The Code is as follows:
Code
Static void Main (string [] args)
{
String datasource = "c:/test. db ";
If (! System. IO. File. Exists (datasource ))
SQLiteConnection. CreateFile (datasource );
SQLiteConnection conn = new SQLiteConnection ();
SQLiteConnectionStringBuilder conStr = new SQLiteConnectionStringBuilder ();
ConStr. DataSource = datasource;
Conn. ConnectionString = conStr. ToString ();
// Open connetcion
Conn. Open ();
SQLiteCommand cmd = new SQLiteCommand ();
String SQL = string. Empty;
Cmd. Connection = conn;
/*
// Create a table
String SQL = "CREATE TABLE test (username varchar (20), password varchar (20 ));";
Cmd. CommandText = SQL;
Cmd. Connection = conn;
Cmd. ExecuteNonQuery ();
*/
// Insert data
For (int I = 0; I <= 10; I ++)
{
SQL = "INSERT INTO test VALUES (cola, mypassword )";
Cmd. CommandText = SQL;
& Nbs