The operation of C # and SQLite

Source: Internet
Author: User
Tags password protection

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 much different.

Create a database file
String datasource= "H:/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 (' Dotnetthink ', ' mypassword ')";
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 ());

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.