(LINQ 学习系列)(6)Linq教程实例: 使用自写类代码来访问数据

本文介绍如何自定义一个与数据表对应的类,并通过Linq进行数据操作。以StudentClass为例,展示了如何映射数据库字段到类属性,并在窗体中加载数据显示。
1.    自定义一个和数据表相对应的类.例如建议StudentClass.cs
    /* *
    *  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.    建一个窗体 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.      运行结果如下图

由上面结果可以看出 , 自己建类和使用 DataContent 建立的方式基本实现的效果一致 . 某些人提出自己写类特别麻烦 , 其实有很多工具可以生产大家所需要的基本类

 

 

 

 

转载于:https://www.cnblogs.com/meetweb/archive/2012/04/04/2432124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值