1. 程式人生 > >DataTable的一個簡單的擴展

DataTable的一個簡單的擴展

ext pty new bsp 擴展 spa lee div inf

我們在調試代碼的時候經常遇到DataTable的數據類型錯誤,這個類可以幫助我們很快查看DataTable的結構信息.

 1 /// <summary>
 2 /// DataTable擴展類
 3 /// </summary>
 4 public static class DataTableExtensions
 5 {
 6     /// <summary>
 7     /// 顯示DataTable的結構信息
 8     /// </summary>
 9     /// <param name="table">datatable</param>
10     public
static void LoadDataTableStructure(this DataTable table) 11 { 12 if (table == null) 13 { 14 System.Diagnostics.Debug.WriteLine("datatable is null."); 15 } 16 17 StringBuilder structureInfo = new StringBuilder(); 18 string colName = string.Empty;
19 string colType = string.Empty; 20 21 structureInfo.AppendLine("============================Begin============================="); 22 structureInfo.AppendLine("TableName: " + table.TableName); 23 structureInfo.AppendLine(string.Format("{0,-20}{1}", "ColumnName", "DataType
")); 24 25 foreach (DataColumn col in table.Columns) 26 { 27 colName = col.ColumnName; 28 colType = col.DataType.ToString(); 29 structureInfo.AppendLine(string.Format("{0,-20}{1}", colName, colType)); 30 } 31 32 structureInfo.AppendLine("=============================End=============================="); 33 System.Diagnostics.Debug.WriteLine(structureInfo.ToString()); 34 } 35 }

DataTable的一個簡單的擴展