zoukankan      html  css  js  c++  java
  • Linq to DataSet 和 DataSet使用方法学习

     简单入门:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace LinqtoDataset
    {
        class Program
        {
            static void Main(string[] args)
            {
                LinqToDataSet.UpdateData();
                LinqToDataSet.PrintData();
               
            }
            class LinqToDataSet
            {
                public static DataSet CreatDataSet()
                {
                    string[] nameSet = { "王霞", "张三", "李四", "李花", "王五", "陆六", "夏七", "吴八" };
                    string[] sexSet = { "", "", "", "", "", "", "", "" };
                    int[] ageSet = { 18, 20, 21, 22, 19, 20, 25, 24 };
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable("People");
                    ds.Tables.Add(dt);
                    dt.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
                    dt.Columns.Add(new DataColumn("Sex", Type.GetType("System.String")));
                    dt.Columns.Add(new DataColumn("Age", Type.GetType("System.Int32")));
                    for (int i = 0; i < nameSet.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Name"] = nameSet[i];
                        dr["Sex"] = sexSet[i];
                        dr["Age"] = ageSet[i];
                        dt.Rows.Add(dr);
                    }
                    return ds;     
                }
                public static void PrintData()
                {
                    DataSet ds = CreatDataSet();
                    DataTable dt = ds.Tables["People"];
                    var query1 = from item in dt.AsEnumerable() select item;
                    foreach (var item in query1)
                    {  
                        Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item["Name"].ToString(), item.Field<string>("Sex"), item.Field<int>("Age"));
                    }
                    var query2 = from item in dt.AsEnumerable() select item.Field<string>("Name");
                    foreach (var item in query2) { Console.WriteLine("姓名:{0}", item); }
                    var query3 = from item in dt.AsEnumerable() where item.Field<int>("Age") >= 22 select item;
                    foreach (var item in query3) { Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item.Field<string>("Name"), item.Field<string>("Sex"), item.Field<int>("Age")); }
                    Console.ReadLine();
                }
                public static void UpdateData()
                {
                    DataSet ds = CreatDataSet();
                    DataTable dt = ds.Tables[0];
                    foreach (var item in dt.AsEnumerable())
                    {
                        int age = item.Field<int>("Age");
                        item.SetField<int>("Age", age + 2);
                    }
                    foreach (var item in dt.AsEnumerable()) { Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item.Field<string>("Name"), item.Field<string>("Sex"), item.Field<int>("Age")); }
                }
            }
        }
    }

    (有待整理!)

     

    http://www.cnblogs.com/guowenhui/archive/2011/12/04/2275484.html

    https://msdn.microsoft.com/zh-cn/library/bb399401.aspx

  • 相关阅读:
    [转]PHP学习入门的一些基础知识
    原来我一直徘徊在选择中
    do while循环学习
    C#装箱
    我的新手学习失败之谈
    C#教程第五课:方法
    转.iPhone开发网站、论坛、博客
    数据库作业Email发送
    安装卸载WINDOWS服务
    SQL SERVER 2008 数据库收缩语句
  • 原文地址:https://www.cnblogs.com/lwngreat/p/4921773.html
Copyright © 2011-2022 走看看