1. Customize a class corresponding to the data table. For example, StudentClass. cs is recommended.
/**
* Meetweb@sohu.com
* Modify By 2012-3
**/
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Data. Linq. Mapping;
Using System. Data;
Using System. Reflection;
Using System. Linq. Expressions;
Using System. ComponentModel;
Namespace LinqTest
{
[Table (Name = "student")]
Class StudentClass
{
Private int _ ID;
Private string _ StudentName;
Private System. Nullable <int> _ Old;
Private string _ Sex;
[Column (Name = "ID")]
Public int ID
{
Set
{
_ ID = value;
}
Get
{
Return _ ID;
}
}
[Column (Name = "StudentName")]
Public string StudentName
{
Get
{
Return this. _ StudentName;
}
Set
{
This. _ StudentName = value;
}
}
[Column (Name = "Old")]
Public System. Nullable <int> Old
{
Get
{
Return this. _ Old;
}
Set
{
If (this. _ Old! = Value)
{
This. _ Old = value;
}
}
}
[Column (Name = "Sex")]
Public string Sex
{
Get
{
Return this. _ Sex;
} Set {
If (this. _ Sex! = Value ))
{
This. _ Sex = value;
}
}
}
}
}
1. Create a form Frmindividuation
Private void Frmindividuation_Load (object sender, EventArgs e)
{
GetData ();
}
// Get The Data Table
Private void GetData ()
{
IDbConnection conn = new SqlConnection (sqlconStr );
Conn. Open ();
DataContext ctx = new DataContext (conn );
Table <StudentClass> dx = ctx. GetTable <StudentClass> ();
This. dataGridView1.DataSource = dx. ToList ();
}
2. The running result is as follows:
The above results show that the effect of self-built classes is basically the same as that of using DataContent. some people put it very troublesome to write their own classes. In fact, there are many tools that can produce the basic classes that everyone needs.