温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

DataTable常识[.NET Framework 2.0]

发布时间:2020-06-13 09:57:59 来源:网络 阅读:422 作者:ly_bing 栏目:编程语言
今天用到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]}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI