Exemple #1
0
        /// <summary>
        /// 根据DataTable获取实体类的字符串
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public string DataTableToClass(DataTable dt, string className, string nameSpace = null, List <PubModel.DataTableMap> dataTableMapList = null)
        {
            StringBuilder reval           = new StringBuilder();
            StringBuilder propertiesValue = new StringBuilder();
            string        replaceGuid     = Guid.NewGuid().ToString();

            foreach (DataColumn r in dt.Columns)
            {
                propertiesValue.AppendLine();
                string typeName = ChangeType(r.DataType);
                bool   isAny    = false;
                PubModel.DataTableMap columnInfo = new PubModel.DataTableMap();
                if (dataTableMapList.IsValuable())
                {
                    isAny = dataTableMapList.Any(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                    if (isAny)
                    {
                        columnInfo = dataTableMapList.First(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                        propertiesValue.AppendFormat(@"     /// <summary>
     /// 说明:{0} 
     /// 默认:{1} 
     /// 可空:{2} 
     /// </summary>
",
                                                     columnInfo.COLUMN_DESCRIPTION.IsValuable() ? columnInfo.COLUMN_DESCRIPTION.ToString() : "-", //{0}
                                                     columnInfo.COLUMN_DEFAULT.IsValuable() ? columnInfo.COLUMN_DEFAULT.ToString() : "-",         //{1}
                                                     Convert.ToBoolean(columnInfo.IS_NULLABLE));                                                  //{2}
                    }
                }
                propertiesValue.AppendFormat("    public {0} {1} {2}", isAny ? ChangeNullable(typeName, Convert.ToBoolean(columnInfo.IS_NULLABLE)) : typeName, r.ColumnName, "{get;set;}");
                propertiesValue.AppendLine();
            }

            reval.AppendFormat(@"   public class {0}{{
                        {1}
   }}
            ", className, propertiesValue);

            if (nameSpace != null)
            {
                return(string.Format(@"using System;
namespace {1}
{{
 {0}
}}", reval.ToString(), nameSpace));
            }
            else
            {
                return(reval.ToString());
            }
        }
        /// <summary>
        /// 根据DataTable获取实体类的字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="nameSpace"></param>
        /// <param name="dataTableMapList"></param>
        /// <returns></returns>
        public string DataTableToClass(DataTable dt, string className, string nameSpace = null, List <PubModel.DataTableMap> dataTableMapList = null)
        {
            StringBuilder reval           = new StringBuilder();
            StringBuilder propertiesValue = new StringBuilder();
            string        replaceGuid     = Guid.NewGuid().ToString();

            var           template        = ClassTemplate.Template;
            string        _ns             = nameSpace;
            string        _foreach        = "";
            string        _className      = className;
            List <string> _primaryKeyName = new List <string>();

            foreach (DataColumn r in dt.Columns)
            {
                propertiesValue.AppendLine();
                string typeName = ChangeType(r.DataType);
                bool   isAny    = false;
                PubModel.DataTableMap columnInfo = new PubModel.DataTableMap();
                if (dataTableMapList.IsValuable())
                {
                    isAny = dataTableMapList.Any(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                    if (isAny)
                    {
                        columnInfo = dataTableMapList.First(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                        if (columnInfo.IS_PRIMARYKEY.ToString() == "1")
                        {
                            _primaryKeyName.Add(r.ColumnName);
                        }
                        propertiesValue.AppendFormat(ClassTemplate.ClassFieldSummaryTemplate,
                                                     columnInfo.COLUMN_DESCRIPTION.IsValuable() ? columnInfo.COLUMN_DESCRIPTION.ToString() : "-", //{0}
                                                     columnInfo.COLUMN_DEFAULT.IsValuable() ? columnInfo.COLUMN_DEFAULT.ToString() : "-",         //{1}
                                                     Convert.ToBoolean(columnInfo.IS_NULLABLE));                                                  //{2}
                    }
                }
                propertiesValue.AppendFormat(
                    ClassTemplate.ItemTemplate,
                    isAny ? ChangeNullable(typeName, Convert.ToBoolean(columnInfo.IS_NULLABLE)) : typeName,
                    r.ColumnName, "{get;set;}",
                    "");
                propertiesValue.AppendLine();
            }
            _foreach = propertiesValue.ToString();

            template = ClassTemplate.Replace(template, _ns, _foreach, _className, _primaryKeyName);
            return(template);
        }