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]
URL链接:http://scyanting.com/article/ihhepp.html