In. net uses SQLite. the wrapper I use here is system. data. SQLite, it only needs one DLL, the interface conforms to ADO. NET 2.0 definition, performance is good, nhib.pdf is also used, currently supports ADO. net 3.5, which can be integrated into vs2005 and vs2008, and supports wince, which is a highlight
Because it complies with the ADO. Net specifications, the usage is basically the same as that of native such as sqlclient and oledb.
Using system. Data;
Using system. Data. SQLite;
//...
Using (sqliteconnection Cn = new sqliteconnection (
"Data Source = test. db3; pooling = true; failifmissing = false ")
)
// When pooling is set to true, the SQL connection is obtained from the connection pool. If not, the SQL connection is created and added to the connection pool. The default value is true.
// The default value of failifmissing is false. If the database file does not exist, a new one is automatically created. If it is set to true, an exception is thrown instead of being created.
{
// When you open the database, it determines whether the database exists. If not, create
CN. open ();
Using (sqlitecommand cmd = new sqlitecommand ())
{
Cmd. Connection = cn;
// Create a table. If the table already exists, an error is returned.
Cmd. commandtext = "create table [test] (ID int, name nvarchar (20 ))";
Cmd. executenonquery ();
// Insert Test Data
For (INT I = 2; I <5; I ++)
{
Cmd. commandtext = string. Format ("insert into [test] values ({0}, 'dos Technology Discussion Region')", I );
Cmd. executenonquery ();
}
For (INT I = 5; I <10; I ++)
{
Cmd. commandtext = string. Format ("insert into [test] values ({0}, 'English test')", I );
Cmd. executenonquery ();
}
// Read data
Cmd. commandtext = "select * from [test]";
Using (sqlitedatareader DR = cmd. executereader (commandbehavior. closeconnection ))
{
While (dr. Read ())
{
Console. writeline ("entry {0}: {1}", dr. getvalue (0), dr. getstring (1 ));
}
}
}
}
Use SQLite in C #
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 ('ekinglong', '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 ());