About. How to use SQLite database in net

Source: Internet
Author: User

Tags:. SH dependent TST Arc recommended create. Data Uil min


SQLite's C # connection to SQLite

The library installation of SQLite is very special:

: Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki--ok!
Https://www.sqlite.org/download.html. ---downloads for Android, Mac, Linux and other platforms.

To download the installation package:
Sqlite-netfx20-setup-bundle-x64-2005- ok!
or Sqlite-netfx45-setup-bundle-x64-2012- need to uninstall version 2.0 before installing. ---test ok!
(in order to be consistent with other software, it is recommended to install the. Net 4.5 version!)

Default installation path: C:\Program files\system.data.sqlite

. NET reference: Just refer to the installation bin directory, System.Data.SQLite.dll a file!


1, with bundles of the expression dynamic library is compiled according to the mixed mode, there are pure x86 and pure x64 mode of the library, a total of 3, according to the actual need to choose. (Consistent with project build-target platform!) )

2, download the library, be sure to install, just line!! (reference only, not installed, run will error!!) )

3,. NET uses SQLite, only need to reference System.Data.SQLite.dll, do not have to sqlite3.dll at all. Didn't think of it!

About SQLite:

SQLite is a lightweight database for local data storage. Open source database.

Advantages, it occupies a very low resource, in the embedded device requires hundreds of K of memory is enough, as a lightweight database, he is fast enough to handle, the capacity level of support is T-level, Independent: no additional dependency; open source; support multiple languages.

Detailed Advantages:

1, its design goal is embedded, it occupies a very low resource, in the embedded device, may only need hundreds of K of memory enough.

2, cross-platform and multi-lingual support: It can support Windows/linux/unix and so on mainstream operating system, and can be combined with many programming languages,
such as C, C + +, PHP, Perl, Java, C#,python, Ruby and so on.

3, fast: Compared to MySQL, PostgreSQL two open-source world-renowned database management system, it processing speed faster than they.
(faster than some popular database operations in most ordinary databases.) )

4, support database size to 2TB.

5, small enough, roughly 130,000 lines C code, 4.43M

6. Simple, Easy API

7, source completely open source, you can use for any purpose, including selling it.

8, it also supports transaction processing functions and so on.

Use. NET Operation Sqllite:

Example code 1:

public string Query ()
String datasource = "E:/tmp/test.db";
System.Data.SQLite.SQLiteConnection.CreateFile (DataSource);
Connecting to a 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 password, SQLite ADO. NET implements the 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 (), password varchar (20))";
Cmd.commandtext = SQL;
Cmd. Connection = conn;
Cmd. ExecuteNonQuery ();
Inserting data
sql = "INSERT into Test VALUES (' A ', ' B ')";
Cmd.commandtext = SQL;
Cmd. ExecuteNonQuery ();
Remove 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 ());

Return SB. ToString ();


Example code 2: Transaction operations:

using (sqliteconnection conn = new Sqliteconnection (connectionString))
Conn. Open ();
Sqlitecommand cmd = new Sqlitecommand ();
Cmd. Connection = conn;
Sqlitetransaction TX = conn. BeginTransaction ();
Cmd. Transaction = TX;
for (int n = 0; n < sqlstringlist.count; n++)
String strSQL = Sqlstringlist[n]. ToString ();
if (strSQL. Trim (). Length > 1)
Cmd.commandtext = strSQL;
Cmd. ExecuteNonQuery ();
Tx.commit ();
catch (System.Data.SQLite.SQLiteException E)
Tx. Rollback ();
throw new Exception (e.message);

About. How to use SQLite database in net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: