C # operations with SQLite

Source: Internet
Author: User
Tags password protection
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 ());

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.