1. Use Add references to reference system. Data. SQLite. dll in the bin directory of the SQLite ADO. net installation directory.
2. Create a database file: because it is always a 0-byte file, I/O can also be used (?!).
System. Data. SQLite. sqliteconnection. createfile (datasource );
3. Connect to the database
System. Data. SQLite. sqliteconnection conn = new system. Data. SQLite. sqliteconnection (connectionstring );
Connectionstring contains some database configuration information, such as database files and password opened by the database. You can use system. Data. SQLite. sqliteconnectionstringbuilder to create connectionstring.
4. Creating tables and reading data are not much different from access or ms SQL.
// Create a database file
String datasource = "H:/test. DB ";
System. Data. SQLite. sqliteconnection. createfile (datasource );
// Connect to the database
System. Data. SQLite. sqliteconnection conn = new system. Data. SQLite. sqliteconnection ();
System. Data. SQLite. sqliteconnectionstringbuilder connstr = new system. Data. SQLite. sqliteconnectionstringbuilder ();
Connstr. datasource = datasource;
Connstr. Password = "admin"; // set the password. SQLite ADO. Net implements database password protection.
Conn. connectionstring = connstr. tostring ();
Conn. open ();
// Create a table
System. Data. SQLite. sqlitecommand cmd = new system. Data. SQLite. sqlitecommand ();
String SQL = "CREATE TABLE test (username varchar (20), password varchar (20 ))";
Cmd. commandtext = SQL;
Cmd. Connection = conn;
Cmd. executenonquery ();
// Insert data
SQL = "insert into test values ('dotnetthink', 'mypassword ')";
Cmd. commandtext = SQL;
Cmd. executenonquery ();
// Retrieve data
SQL = "select * from test ";
Cmd. commandtext = SQL;
System. Data. SQLite. sqlitedatareader reader = cmd. executereader ();
Stringbuilder sb = new stringbuilder ();
While (reader. Read ())
{
SB. append ("username:"). append (reader. getstring (0). append ("\ n ")
. Append ("Password:"). append (reader. getstring (1 ));
}
MessageBox. Show (sb. tostring ());