Use of LINQ to SQLite

Source: Internet
Author: User

Databases are an indispensable part of software. Traditional databases are getting bigger and bigger, which is a waste of Small and Medium applications. SQLite is a lightweight database that supports acid transactions without installation or configuration,Source codeIt is completely open and fast. It is very suitable for small and medium-sized applications. It is an easy-to-use architecture developed by Microsoft to generate and manage relational databases; using linq to the SQLite database will bring great convenience to the programming of the SQLite database.

Refer to: LINQ to SQLite Practice Guide;
Some problems encountered when installing SQLite provider in. NET Framework 4.0

First, download the requiredProgram: System. Data. SQLite and dblinq2007. The first one is ADO. net provider and LINQ provider of SQLite, and dblinq2007 is the software for generating data context from LINQ to SQLite.

Create SQLite databases and add tables. SQLite has many excellent GUI management software. Here I use sqlite2009 pro. The software interface is as follows:

Create two tables: Application and user tables. Each table has only two fields to display the usage of LINQ to SQLite. This database is located in the debug folder of the project folder. Used for program running connection.

Install system. data. SQLite; decompress dblinq2007, and place it in the dblinq folder of drive D; To system. data. in the SQLite installation folder, copy system. data. SQLite. DLL files are stored in the dblinq2007 folder and need to be used during running;

Then the datacontext is generated. Run the command dbmetal.exe/Database: Data/namespace = linqtosqlite/code = test on the console. CS/provider = SQLite/Conn = "Data Source = D: \ Users. db3"

The red labels are respectively the datacontext class name, namespace name, file name, and database path.

Create a project, add an existing file: The file generated just now, add reference (dblinq. DLL, dblinq. SQLite. DLL: In dblinq2007, system. data. SQLite. DLL, system. data. SQLite. LINQ. DLL: Located in system. data. in the SQLite installation folder, system. data. LINQ); system. data. LINQ is located in. net, such:

A problem occurs when you connect to the database using the datacontext object (the usage of LINQ): The cause is that the connection string is directly written into the constructor for convenience. For example: datacontext (@ "Data Source = your_sqlite_db_path"); in this way, there will be a problem with the connection, no response for a long time or an exception is thrown. The solution is to create a sqliteconnection explicitly, such: datacontext (New sqliteconnection (@ "Data Source = your_sqlite_db_path"); this is what I use in the program.

Then, you can use LINQ to SQLite normally.

VaRQuery =FromUInTC. UserWhereU. ID = textbox2.textSelectU;Foreach(UserUInQuery) {textbox1.text + ="User name :"+ U. ID +""+"Password :"+ U. Password +"\ R \ n";}

In addition, dotconnect for SQLite can be used as the ADO of SQLite.. net provider, which supports LINQ to SQLite. If it is used, it is not so troublesome. However, it is a commercial software and provides a free version for use. For details about the methods, refer to: the actual situation of LINQ to SQLite, the process is detailed.

Are there any shortcomings? Thank you!

Attachment: Sample project

Author:Give me a drink

Source: http://Engin.cnblogs.com/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. repost the text and indicate the source. Thank you.

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.