zoukankan      html  css  js  c++  java
  • (LINQ 学习系列)(6)Linq教程实例: 使用自写类代码来访问数据

    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建立的方式基本实现的效果一致. 某些人提出自己写类特别麻烦,其实有很多工具可以生产大家所需要的基本类

  • 相关阅读:
    51Nod 1016 水仙花数 V2(组合数学,枚举打表法)
    April Fools Contest 2017 题解&源码(A,数学 B,数学 C,数学 D,字符串 E,数字逻辑 F,排序,卡时间,G,数学)
    统计0到n之间1的个数[数学,动态规划dp](经典,详解)
    hihoCoder #1082 : 然而沼跃鱼早就看穿了一切(字符串处理)
    洛谷 P1876 开灯(思维,枚举,规律题)
    Codeforces 789A Anastasia and pebbles(数学,思维题)
    51Nod 1182 完美字符串(字符串处理 贪心 Facebook Hacker Cup选拔)
    机器学习(Machine Learning)&深度学习(Deep Learning)资料
    看一下你在中国属于哪个阶层?
    python读取mnist
  • 原文地址:https://www.cnblogs.com/meetweb/p/2432124.html
Copyright © 2011-2022 走看看