(LINQ Linq) (6) Examples of the LINQ Tutorial: Using Self-writing code to access data

Source: Internet
Author: User
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.

 

 

 

 

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.