DataTable常识[.NETFramework2.0]

今天用到DataTable时就顺便将DataTable中的一些常识记录下来,为便备查。
为此写了个很easy的例子,例子是为我们这些rookie准备的.
往高手就多多指点例子中的不足,
包含:
1:创建DataTable
2:如何使用for
3:如何使用foreach

DataTable常识[.NET Framework 2.0]static void Main(string[] args)
DataTable常识[.NET Framework 2.0]{
DataTable常识[.NET Framework 2.0]        DataTable dt = InitialData();
DataTable常识[.NET Framework 2.0]
        //查找 DataTable 中 A列 值 大于中 A_3 的列
DataTable常识[.NET Framework 2.0]        DataRow[] drs = dt.Select("A > 'A_3'", "A desc");
DataTable常识[.NET Framework 2.0]
        Console.WriteLine("A\tB\tC\tD");
DataTable常识[.NET Framework 2.0]
        //遍历数据
DataTable常识[.NET Framework 2.0]        foreach (DataRow row in drs)
DataTable常识[.NET Framework 2.0]        {
DataTable常识[.NET Framework 2.0]                string a = row["A"] as string;
DataTable常识[.NET Framework 2.0]                string b = row["B"] as string;
DataTable常识[.NET Framework 2.0]                string c = row["C"] as string;
DataTable常识[.NET Framework 2.0]                string d = row["D"] as string;
DataTable常识[.NET Framework 2.0]                Console.WriteLine(a + "\t" + b + "\t" + c + "\t" + d);
DataTable常识[.NET Framework 2.0]        }
DataTable常识[.NET Framework 2.0]
        Console.ReadKey();
DataTable常识[.NET Framework 2.0]}
DataTable常识[.NET Framework 2.0]
/// <summary>
/// 初始化测试数据
/// </summary>
DataTable常识[.NET Framework 2.0]public static DataTable InitialData()
DataTable常识[.NET Framework 2.0]{
DataTable常识[.NET Framework 2.0]        //创建一个名为:TestTable的数据表
DataTable常识[.NET Framework 2.0]        DataTable dt = new DataTable("TestTable");
DataTable常识[.NET Framework 2.0]
        //为表创建 4列,分别为: A 、 B 、 C 、 D
DataTable常识[.NET Framework 2.0]        dt.Columns.Add(new DataColumn("A"));
DataTable常识[.NET Framework 2.0]        dt.Columns.Add(new DataColumn("B"));
DataTable常识[.NET Framework 2.0]        dt.Columns.Add(new DataColumn("C"));
DataTable常识[.NET Framework 2.0]        dt.Columns.Add(new DataColumn("D"));
DataTable常识[.NET Framework 2.0]
        for (int i = 1; i <= 10; i++)
DataTable常识[.NET Framework 2.0]        {
DataTable常识[.NET Framework 2.0]                //添加新的数据行,并添加数据
DataTable常识[.NET Framework 2.0]                DataRow dr = dt.NewRow();
DataTable常识[.NET Framework 2.0]                dr["A"] = "A_" + i;
DataTable常识[.NET Framework 2.0]                dr["B"] = "B_" + i;
DataTable常识[.NET Framework 2.0]                dr["C"] = "C_" + i;
DataTable常识[.NET Framework 2.0]                dr["D"] = "D_" + i;
DataTable常识[.NET Framework 2.0]
                //将此行添加到DataTable中
DataTable常识[.NET Framework 2.0]                dt.Rows.Add(dr);
DataTable常识[.NET Framework 2.0]        }
DataTable常识[.NET Framework 2.0]
        return dt;
DataTable常识[.NET Framework 2.0]}

网站栏目:DataTable常识[.NETFramework2.0]
URL分享:https://www.cdcxhl.com/article38/ihhepp.html

成都网站建设公司_创新互联,为您提供标签优化品牌网站设计外贸网站建设网站设计服务器托管全网营销推广

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化