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