20150221-linq to SQL query data

Source: Internet
Author: User
Tags dashed line

LINQ to SQL can query SQL-based data quickly, including the basic object/relation mapper directly in VS, and the O/R mapper can quickly map SQL-based data sources to CLR objects and then use LINQ queries.

In the project, right-click Add-New item to find the LINQ to SQL class

Then create a new connection and click Connect to the database:

Add Connection

In the service management resource on the left, find the connection name you want to add, table name

Drag the table to DataClasses1 and click Yes

Drag and drop the required tables as needed:

Tables that have a primary foreign key relationship have a dashed line between them.

Query with LINQ, add two controls to the form: a button and a GridView

Write the code in the button:

Private void button1_click (object  sender, EventArgs e)        {            new Dataclasses1datacontext (); // initializing LINQ to SQL Classes             // Universal variable var. Query statement            varfrom inselect  m;             // bind a data source            to GridView1 this. Datagridview1.datasource = que;        }

Show Results:

The table header names are the column names in the table, but you can change the text that the header displays:

Click the small arrow in the upper-right corner of the GridView--Edit column:

On the right side of the data column, DataPropertyName corresponds to the column name of the table, in the Appearance column, HeaderText corresponds to the header name displayed.

Conditional query:

Private void button2_click (object  sender, EventArgs e)        {            new Dataclasses1datacontext (); // Initialize            var  from inch where " male " Select m;             this. Datagridview1.datasource = op;        }

20150221-linq to SQL query data

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.