2, datacontext

Source: Internet
Author: User

Summary:

Datacontext is used to convert a LINQ statement into a T-SQL statement to realize the functions of database operations such as query and writing. In the system. Data. LINQ namespace, datacontext also provides the logging function.

Content:

A small example of datacontext:

1. We will create a new leleapplication. At this time, there is no system. Data. LINQ; in the reference space. We need to introduce the type ourselves.

 

2. Create an object class and introduce system. Data. LINQ. Mapping.

[Table (name = "user")] // defines the name of the table in the database.

Public class user

{

[Column (isprimarykey = true, name = "log")] // defines the primary key and the name in the database

Public String login {Get; set ;}

[Column] // Add a feature. The default name is PWD.

Public int PWD {Get; set ;}

}

For the attribute of a feature, the following message is displayed:

 

3. for related data connections, introduce using system. Data. LINQ; using datacontext.

Datacontext dt = newdatacontext ("Data Source =

. \ Sqlexpress; attachdbfilename = c: \ Documents ents and settings

\ Administrator \ Desktop \ LINQ \ sqdemo2datacontext \ sqdemo2datacontext \\

Lqdemo2datacontextdb. MDF; integratedsecurity = true; Connect timeout = 30;

User instance = true ");

Table <user> Users = DT. gettable <user> ();

VaR user = from u in users selectu. login;

Foreach (var u in user)

Console. writeline (U );

Console. readkey ();

Datacontext dt = new datacontext ("database connection ");

You can also introduce using system. Data. sqlclient; to separate the connection path:

Sqlconnectionconn = new sqlconnection ("connection ");

Datacontext dt = new datacontext (conn );

Run the command and the result is the same:

 

 

 

Log function:

It can help you record the execution of all the SCSI statements you want to record, but records the SQL statements converted.

Using system. IO;

Datacontext dt = newdatacontext ("connection ");

Table <user> Users = DT. gettable <user> ();

Streamwritersw = new streamwriter ("log.txt", true );

DT. log = Sw;

VaR user = from u in users selectu. login;

Foreach (var u in user)

Console. writeline (U );

Sw. Close ();

Console. readkey ();

After running

The log.txt file is available under bin \ debug. At this time, the log records the SQL statements converted by all the SCSI statements related to the DT connection.

 

The record is very simple and useful when debugging programs and testing programs.

 

Datacontext also provides SQL query capabilities:

The following SQL statement can be used to replace the following statement when the SCSI statement is very complex or cannot be completed:

DT. executecommand ("updata ......");

Ienumerable <user> User = DT. executequery <user> ("Select log from user.

However, during the experiment, I always prompt that there is an error near the SQL statement user. I don't know what's going on.

 

Summary:

Datacontext also provides many methods and attributes, such as creating a database in createdatabase, deleting a database in deletedatabase, and using other data sources in translate.

 

Download source code here

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.