In. NET using SQLite, I use the wrapper here is System.Data.SQLite, it only needs a DLL, interface conforms to the definition of ADO 2.0, performance is also good, nhibernate with it, currently support ADO 3.5, support integration in VS2005 and VS2008 inside, and support wince, is a bright spot
Because it complies with the Code of ADO, so the use way, basic and SqlClient, OLE DB and other native consistent
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, and if none is new and added to the connection pool, the default is true. Failifmissing defaults to False, and if the database file does not exist, it automatically creates a new one that, if set to true, will not be created, but throws exception information.
{//When the database is opened, the database is determined to exist, and if it does not exist, a CN is created under the current directory. Open ();
using (sqlitecommand cmd = new Sqlitecommand ())) {cmd. Connection = CN;
CREATE TABLE, if the table already exists, then error 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}, ' Dostoyevsky Wave technology Discussion area ')", I); Cmd. ExecuteNonQuery (); }
for (int i = 5; i < ten; i++) { Cmd.commandtext = string. Format ("INSERT into [test] VALUES ({0}, ' 中文版 test ')", I); cmd. ExecuteNonQuery (); }
Read Data Cmd.commandtext = "SELECT * FROM [Test]"; using (Sqlitedatareader dr = cmd. ExecuteReader (CommandBehavior.CloseConnection)) {while (Dr. Read ()) {Console.WriteLine ("{0}: {1}", Dr. GetValue (0), Dr. GetString (1)); } } } }
Using SQLite in C #
1. Refer to SQLite ADO by add references. NET installation directory in the bin directory of the System.Data.SQLite.DLL.
2, create the database file: Because it is always a 0-byte file, you should use IO can also (?!). )。
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 configuration information of the database, such as database file, database open password, etc. You can use System.Data.SQLite.SQLiteConnectionStringBuilder to assist in creating connectionstring
4, creating tables, reading data, and so on and access or MS SQL is not very much different.
//Create a database file StringDataSource="H:/test.db"; System.Data.SQLite.SQLiteConnection.CreateFile (DataSource);//Connecting to a database System.Data.SQLite.SQLiteConnection Conn=NewSystem.Data.SQLite.SQLiteConnection (); System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr=NewSystem.Data.SQLite.SQLiteConnectionStringBuilder (); ConnStr. DataSource=DataSource ConnStr. Password="Admin";//Set password, SQLite ADO. NET implements the database password protection Conn. ConnectionString=ConnStr. ToString (); Conn. Open ();//Create a table System.Data.SQLite.SQLiteCommand cmd=NewSystem.Data.SQLite.SQLiteCommand ();StringSql="CREATE TABLE Test (username varchar), password varchar (20))"; Cmd.commandtext=sql Cmd. Connection=Conn Cmd. ExecuteNonQuery ();//Inserting data Sql="INSERT into Test VALUES (' Ekinglong ', ' MyPassword ')"; Cmd.commandtext=sql Cmd. ExecuteNonQuery ();//Remove data Sql="SELECT * FROM Test"; Cmd.commandtext=sql System.Data.SQLite.SQLiteDataReader Reader=Cmd. ExecuteReader (); StringBuilder SB=NewStringBuilder ();While(Reader. Read ()){sb. Append ("username:). Append (reader. GetString (0 "/n" ) . Append ( "password:" Span style= "color: #000000;" >). Append (reader. GetString (1 messagebox.show (sb.) ToString ());
"reproduced in. NET using SQLite"