https://www.devart.com/dotconnect/sqlite/articles/tutorial_linq.html
LINQ to sqlite Tutorial@H_403_17@
This tutorial guides you through the process of creating a simple application powered byLinqConnecttechnology. In less than 5 minutes you will have a ready-to-use data access layer for your business objects.
In this walkthrough:
- Introducing the LinqConnect (LINQ to SQLite) Technology
- Requirements
- Preparing the Project
- Generating Model from Database
- Querying Data
- Inserting New Data
- Updating Data
- Deleting Data
- Additional Information
Introducing the LinqConnect (LINQ to sqlite) Technology
LinqConnect (formerly known as LINQ to sqlite) is the fast and lightweight ORM solution,which is closely compatible to Microsoft LINQ to sql and contains its own advanced features,such as complex type support,advanced data fetching options,configurable compiled query caching,and others.
LINQ stands for Language-Integrated Query,which means that data retrieval is no longer a separate language. The LINQ engine allows .NET applications to connect to databases without bothering much about columns and rows. The data you receive is automatically formed as objects ready to use by your business logic.
LINQ to Relational Data may be thought of as an object-relational mapping (ORM) tool. The type-safe LINQ queries get compiled into MSIL on the fly,and the query clauses are translated into sql and sent to sqlite database for execution. This makes your data access layer safer,faster,and greatly more convenient to design.
Requirements
In this tutorial it is assumed that you already have the database objects created. You have to execute a script from the following file installed by default to
\Program Files\Devart\dotConnect\sqlite\Samples\crm_demo.sql
Preparing the Project
Create a new console application in Visual Studio. It could be any other project type as well,but for simplicity's sake we'll use console project throughout the tutorial. The rest of the tutorial assumes that the name of the project isConsoleApplication1. If you project is named otherwise,you will have to substitute this name with the actual one in Solution Explorer.
Generating Model from Database
- Add Devart LinqConnect Model to the project. To do this,right-click on the project node in Solution Explorer,point toAdd,clickNew Item.... In theAdd New Itemdialog selectDatacategory,chooseDevart LinqConnect Modeltemplate,and clickAdd. This automatically launches Create New Model wizard,which creates a new empty model or generates it from database.
- ClickNexton the welcome screen.
- Fill in connection settings and clickNext.
- Choose database objects that will be used in the model. These are all objects from the crm_demo script,including auxiliary tables. ClickNext.
- On the next screen you can adjust naming rules for entities and their members. For the CRM Demo database no rules are required,so just clickNext.
- InputCrmDemoContextas namespace,andCrmDemoDataContextas the name of DataContext descendant. This will be the name of the main data access class. ClickNext.
- PressFinish. The model will be generated and opened in Entity Developer.
- In the main menu,clickFile|Save. This updates the generatedCrmDemoDataContextmodel code in Visual Studio.
The model you've just generated is ready to use.
Entity Developer creates classes for all selected tables that represent entities. It also creates a descendant ofDevart.Data.Linq.DataContextclass,which controls the connection to the database,and the whole data flow. This class includes properties and methods named after your database objects. You will use these members to retrieve and modify data in the context. The generated code is contained in the file DataContext1.Designer.cs (DataContext1.Designer.vb). You may write your own partial classes and methods for it in the file DataContext1.cs (DataContext1.vb).
Querying Data
All LINQ to sqlite operations are executed through the DataContext descendant,which is namedCrmDemoDataContextin this tutorial. To retrieve data you have to first create an instance of the context,then prepare a query withLinqConnect,and then access the object returned by the query,which may be a collection of objects or a single object.
Let's read all the data from the table Company,sort it by CompanyID,and output some columns. Add the following block of code to the method Main:
C#
CrmDemoDataContext context =
new
CrmDemoDataContext();
var query = from it
in
context.Companies
orderby it.CompanyID
select it;
foreach
(Company comp
query)
Console.WriteLine(
"{0} | {1} | {2}"
,comp.CompanyID,comp.CompanyName,comp.Country);
Console.ReadLine();
|
Dim
context
As
CrmDemoDataContext =
New
CrmDemoDataContext
query = From it
In
context.companies _
Order By it.CompanyID _
Select
it
comp
company
For
Each
query
Console.WriteLine(
"{0} | {1} | {2}"
Next
Console.ReadLine()