csharp: datatable get Column datatype or Column Name

时间:2021-02-10 11:40:05
/// <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; }