码迷,mamicode.com
首页 > 其他好文 > 详细

csharp: datatable get Column datatype or Column Name

时间:2015-04-09 19:17:32      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

/// <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;

        }

  

csharp: datatable get Column datatype or Column Name

标签:

原文地址:http://www.cnblogs.com/geovindu/p/4410174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!