大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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]
///
/// 初始化测试数据
///

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]
浏览地址:http://dzwzjz.com/article/ihhepp.html
在线咨询
服务热线
服务热线:028-86922220
TOP