This article describes the method of calling SQLite in C#. Share it for your reference. The specific analysis is as follows:
1. Introduction to SQLite:
When we use massive data, we usually use Oracle, SQL Server, DB2, Sybase, MySQL and other databases to save and manage data. If only a small amount of data needs to be saved in the program, it is directly integrated into the registry or saved to an XML file. Then if the amount of data is just not too much, it is a bit of a big deal to use a database like Oracle, and there is no need to. If there is an XML storage speed, it is slower. How to do it? At this time, using SQLite, a small embedded database, is a very ideal choice. It is also very simple and convenient to use.
SQLite does not need to be installed, configured, and then server-side client-side like databases. It is very simple, just directly use a small file, a file with db as the suffix. The size is only a few dozen K. You don't have to do anything else, just copy it and use it directly. It's like operating an ordinary txt file. But it feels a bit inappropriate to treat it as a file. We should understand it this way, it's a bit like a library function, or COM component, dll. Then it provides some interfaces for you to call it. SQLite is open source. If you want to download it and view its C source code, you can go to the official website/
Of course, some people will ask what if we don’t call SQLite through an interface in a program, but only operate through a graphical interface like a general database? You can use a tool called SQLiteBrowser to download and decompress. Without installing it, just double-click the exe file inside to open a graphical interface. Then click the menu File --> open database to find the db file. Then you can view the data in the table and create a new table on the graphical interface. However, SQLite has no permission control, and there is no user name and password, and anyone can open it. So if you save any secret information, it is best to encrypt it first and then save it.
SQLite is developed in C language, so it is no problem to call it with C and C++. However, it can also be called with C#, but it only needs to use a dll. Here I will talk about how to call SQLite with C#.
2. C# calls SQLite
1. First, you have to download a file called online
2. Just like adding other dlls, add this dll first
3. Add namespace using
4. The next thing is to write the code
string connectString = @"Data Source=D:\;Pooling=true;FailIfMissing=false"; /*D:\is the directory where the sqlite database is located. You can change its name as you like*/ SQLiteConnection conn = new SQLiteConnection(connectString); // Create a new connection(); //Open the connection, if it exists, it will be opened normally.//Create a file if it does not existSQLiteCommand cmd = (); = "select * from orders"; //There must be an order table in advance in the database = ; using (SQLiteDataReader reader = ()) { while (()) ( reader[0].ToString()); }
The usage is actually similar to that of ordinary databases that use C# to operate.
In addition, if you want to use Linq, you must use another dll file.
I hope this article will be helpful to everyone's C# programming.