你的位置:首页 > ASP.net教程

[ASP.net教程]csharp: datatable get Column datatype or Column Name


/// <summary>    ///列表名    /// </summary>    /// <param name="table"></param>    /// <returns></returns>    public static string GetColumnNames(System.Data.DataTable table)    {      if (table != null)      {        List<string> lstColumn = new List<string>();        foreach (System.Data.DataColumn col in table.Columns)        {          lstColumn.Add(col.ColumnName);        }        return String.Join(",", lstColumn.ToArray());      }      return string.Empty;      //foreach (DataRow row in table.Rows)      //{      //  foreach (DataColumn column in table.Columns)      //  {      //    ColumnName = column.ColumnName;      //    ColumnData = row[column].ToString();      //  }      //}    }    /// <summary>    /// 数据类型    /// </summary>    /// <param name="table"></param>    /// <returns></returns>    public static string GetColumnNamesType(System.Data.DataTable table)    {      if (table != null)      {        List<string> lstColumn = new List<string>();        foreach (System.Data.DataColumn col in table.Columns)        {          lstColumn.Add(table.Columns[col.ColumnName].DataType.ToString());        }        return String.Join(",", lstColumn.ToArray());      }      return string.Empty;          }    /// <summary>    /// 列名和数据类型    /// </summary>    /// <param name="table"></param>    /// <returns></returns>    public static DataTable GetColumnDataNamesType(System.Data.DataTable table)    {      DataTable dt = new DataTable();      dt.Columns.Add("name", typeof(string));      dt.Columns.Add("datatype", typeof(string));      if (table != null)      {               foreach (System.Data.DataColumn col in table.Columns)        {                    dt.Rows.Add(col.ColumnName, table.Columns[col.ColumnName].DataType.ToString());        }              }      return dt;    }