龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

.NET实现把DataSet填充DataTable

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
DataSet填充DataTable的例子,不用数据库,附NET2.0的DataTableReader快速读取数据 C# Code [http://www.xueit.com] using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data

DataSet填充DataTable的例子,不用数据库,附NET2.0的DataTableReader快速读取数据

 

C# Code [http://www.xueit.com]
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataSetTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*******Fun with DataSets *********\n");
           
            DataSet ds = new DataSet("tb_Employee");
            ds.ExtendedProperties["TimerStamp"] = DateTime.Now;
            ds.ExtendedProperties["Company"] = "www.sina.com";

            DataColumn EmployeeIDColumn = new DataColumn("EmployeeID", typeof(int));
            EmployeeIDColumn.Caption = "员工编号";
            EmployeeIDColumn.ReadOnly = true;
            EmployeeIDColumn.AllowDBNull = false;
            EmployeeIDColumn.Unique = true;
            EmployeeIDColumn.AutoIncrement = true;
            EmployeeIDColumn.AutoIncrementSeed = 0;
            EmployeeIDColumn.AutoIncrementStep = 1;

            DataColumn EName = new DataColumn("EName",typeof(string));
            DataColumn ESex = new DataColumn("ESex", typeof(string));
            DataColumn EAge = new DataColumn("EAge", typeof(int));
            DataColumn EPlace = new DataColumn("EPlace", typeof(string));
            DataColumn EMoney = new DataColumn("EMoney", typeof(int));
            DataTable dt = new DataTable("tb_Employee");
            dt.Columns.AddRange(new DataColumn[] { EmployeeIDColumn,EName, ESex, EAge, EPlace, EMoney });

            DataRow dr = dt.NewRow();
            Console.WriteLine("Row State is:{0}", dr.RowState);
            dr["EName"] = "huangjshhhh";
            dr["ESex"] = "";
            dr["EAge"] = 20;
            dr["EPlace"] = "上海";
            dr["EMoney"] = 10000;
            dt.Rows.Add(dr);

            //add
            Console.WriteLine("Row State is:{0}", dt.Rows[0].RowState);
            dr = dt.NewRow();
            dr["EName"] = "chengshuang";
            dr["ESex"] = "";
            dr["EAge"] = 20;
            dr["EPlace"] = "北京";
            dr["EMoney"] = 10000;
            dt.Rows.Add(dr);

            dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
            ds.Tables.Add(dt);
            PrintDataSet(ds);

           //使用.NET2.0的DataTableReader

           PrintTable(dt);

            Console.ReadLine();
        }

        //使用.NET2.0的DataTableReader
        private static void PrintTable(DataTable dt)
        {
            Console.WriteLine("\n******Rows in DataTable********");
            //得到新的.NET 2.0 DataTableReader类型
            DataTableReader dtReader = dt.CreateDataReader();
            while(dtReader.Read())
            {
                for (int i = 0; i < dtReader.FieldCount; i  )
                {
                    Console.Write("{0}={1}",dtReader.GetName(i),dtReader.GetValue(i).ToString().Trim());
                 Console.WriteLine();
                }
               
            }
            dtReader.Close();
        }

        private static void PrintDataSet(DataSet ds)
        {
            Console.WriteLine("Tables in '{0}' DataSet.\n",ds.DataSetName);
            foreach(DataTable dt in ds.Tables)
            {
                Console.WriteLine("{0} Table.\n", dt.TableName);
                for (int curCol = 0; curCol < dt.Columns.Count;curCol   )
                {
                    Console.Write(dt.Columns[curCol].ColumnName.Trim()   "\t");

                }
                Console.WriteLine("\n");
                for (int curRow = 0; curRow < dt.Rows.Count; curRow  )
                {
                    for (int curCol = 0; curCol < dt.Columns.Count;curCol   )
                    {
                        Console.Write(dt.Rows[curRow][curCol].ToString() "\t");
                    }
                    Console.WriteLine("\n");
                }
                Console.WriteLine();
            }
           
        }
    }
}

使用.NET2.0的DataTableReader的方式和操作数据提供器的数据读取器对象差不多,当想要快速从DataTable中取出数据时,DataTableReader是一个理想的解决方案,它不需要手动遍历DataTable内的行列集合.

精彩图集

赞助商链接