SQLite introduction, C # Calling SQLite

Source: Internet
Author: User
Tags sqlite database

When we use massive data, we generally use Oracle, SQL Server, DB2, Sybase, MySQL and other databases to store and manage data. if only a small amount of data needs to be saved in the program, it can be directly integrated into the registry or saved to an XML file. if there is not much data, it is not necessary to use a database like Oracle. if an XML file is saved, the access speed is slow. what about it? At this time, using SQLite, a small embedded database, is an ideal choice. It is easy and convenient to use.

SQLite does not need to be installed and configured like Oracle or other databases, and then what is the Server Client. it is very simple to directly use a small file with the suffix dB. the size is dozens of K. you don't have to do anything else. Just copy it and use it directly. like operating a common TXT file. however, it is still a bit inappropriate to treat it as a file. we should be able to understand that it is a bit like library functions, COM components, DLL. then we provide some interfaces for you to call .. SQLite is open source, you want to download it and view its C source code can go to the official website http://www.sqlite.org/

Of course, some people will ask, what if we don't call SQLite through an interface in a program, but just operate through a graphical interface like a general database? You can use a tool called sqlitebrowser to download and decompress the tool without installation. Double-click the EXE file to open a graphical interface. click File> open database in the menu, and find the DB file. then you can view the data in the table on the graphic interface and create a new table. however, SQLite does not have any permission control, and no user name or password can be used by anyone. therefore, it is best to encrypt the confidential information before saving it.

SQLite is developed in C language, so calling with C and C ++ is not a problem at all. however, C # can also be called, but a DLL is used. Here I will explain how to use C # To Call SQLite.

 

C # Call SQLite

1. First download a file named system. Data. SQLite. dll from the Internet.

2. add reference to add this DLL just like adding other DLL

3. Add the namespace using system. Data. SQLite

4. write the code.

String connectstring = @ "Data Source = D: \ SQLite. DB; pooling = true; failifmissing = false ";/* D: \ SQLite. DB is the directory where the SQLite database is located. You can change its name as needed */

Sqliteconnection conn = new sqliteconnection (connectstring); // create a connection

Conn. open (); // open the connection. If SQLite. DB exists, it is opened normally. If not, a SQLite. DB file is created.

Sqlitecommand cmd = conn. createcommand ();

Cmd. commandtext = "select * from orders"; // you must have an orders table in the database.

Cmd. commandtype = commandtype. text;

Using (sqlitedatareader reader = cmd. executereader ())

{

While (reader. Read ())

Console. writeline (Reader [0]. tostring ());

}

 

The usage is similar to that of C # databases.

In addition, if you want to use LINQ, you need to use another DLL file, system. Data. SQLite. LINQ. dll.

 

 

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.