using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Threading; namespace CAlianxe1 { class Program { static void Main(string[] args) { #region 创建内存中的Table //Columns对象有两个方法用来添加对应的列 //1:Add,用来添加单个的column实例 //2:AddRange,用来添加一个对象数组 Console.WriteLine(typeof(int)); DataTable dt = new DataTable("shuji"); dt.Columns.Add(new DataColumn("书名", typeof(string))); dt.Columns.Add(new DataColumn("书号", typeof(int))); DataColumn[] dc = new DataColumn[2]; dc[0] = new DataColumn("价格", typeof(decimal)); dc[1] = new DataColumn("出版社", typeof(string)); dt.Columns.AddRange(dc); DataRow dr = dt.NewRow(); dr["书名"] = "c# 编程指南"; dr["书号"] = 111111; dr["价格"] = 98.392398393; dr["出版社"] = "清华大学出版社"; dt.Rows.Add(dr); dt.Rows.Add(new object[] { "C++编程指南", 222222, 29.392398393, "人民教育出版社" }); DisplayResult(dt); DataSet ds = new DataSet(); ds.Tables.Add(dt); ds.Tables.Remove(dt);//移除某一个热顶的表 ds.Tables.RemoveAt(0);//移除指定位置的表 #endregion Thread.Sleep(50000); } static void DisplayResult(DataTable dt) { for (int i = 0; i < dt.Columns.Count; i++) { Console.Write(dt.Columns[i].ColumnName.PadRight(16)); } Console.WriteLine(); for (int i = 0; i < dt.Rows.Count; i++) { foreach (DataColumn d in dt.Columns) { Console.Write("{0}", dt.Rows[i][d].ToString().PadRight(16)); } Console.WriteLine(); for (int j = 0; j < dt.Columns.Count; j++) { Console.Write("{0}", dt.Rows[i][j].ToString().PadRight(16)); } Console.WriteLine(); } } } }