<#@ include file="$(SolutionDir)\CNet.DBUtility\DbHelper\MsSqlDbHelper.ttinclude" #> <#@ include file="$(SolutionDir)\CNet.DBUtility\DbHelper\MySqlDbHelper.ttinclude" #> <#+ public class Config { //数据库类型 public static readonly string DbType = "MySql";//MsSql\MySql //数据库名sqlserver //public static readonly string Server = ".";//"127.0.0.1"; //public static readonly string DbDatabase = "CNet"; //public static readonly string Uid = "sa"; //public static readonly string Pwd = "123123"; //数据库连接 sqlserver //public static readonly string ConnectionString = string.Format("server={0};database={1};uid={2};pwd={3}", Server, DbDatabase, Uid, Pwd); //数据库名mysql public static readonly string Server = "localhost;port=3306";//"127.0.0.1"; public static readonly string DbDatabase = "CNet"; public static readonly string Uid = "root"; public static readonly string Pwd = "123123"; public static readonly string ConnectionString = string.Format("server=localhost;database=CNet;uid=root;pwd=123123;charset=utf8;"); //命名空间 public static readonly string Namespace = "CNet.Main"; //需要生成的表名,多张表用,分割 public static readonly string Tables = null;//"users,rows,titles" } public class DbFactory { public static BaseDbHelper CreatDb(string dbType) { switch (dbType) { case "MsSql": { return new MsSqlDbHelper(); } case "MySql": { return new MySqlDbHelper(); } default: return null; } } } public abstract class BaseDbHelper { public abstract string PreParameter { get; set; } public abstract List GetDbTables(); public abstract List GetDbColumns(string tableName, string schema); public abstract DataTable GetDataTable(string commandText, params IDataParameter[] parms); public abstract string GetParStr(List dbComList); //转为大驼峰命名cts public string ToCamelCase(string inputStr) { var str = string.Empty; var inputStrArr = inputStr.Split('_'); foreach (var item in inputStrArr) { str += item.Substring(0,1).ToUpper()+item.Substring(1)+"_"; } str=str.TrimEnd('_'); return str; } public string GetFileStr(List dbColList) { StringBuilder fields = new StringBuilder(); foreach (DbColumn column in dbColList) { if (!column.IsIdentity) { fields.Append(column.ColumnName); fields.Append(","); } } fields.Remove(fields.Length - 1, 1); return fields.ToString(); } public string GetFileStrWithPrimaryKey(List dbColList) { StringBuilder fields = new StringBuilder(); foreach (DbColumn column in dbColList) { fields.Append(column.ColumnName); fields.Append(","); } fields.Remove(fields.Length - 1, 1); return fields.ToString(); } protected string BGetParStr(List dbColList, string parStr) { StringBuilder fields = new StringBuilder(); foreach (DbColumn column in dbColList) { if (!column.IsIdentity) { fields.Append(parStr + column.ColumnName); fields.Append(","); } } fields.Remove(fields.Length - 1, 1); return fields.ToString(); } public List DtColToList(DataTable dt, ISqlServerDbTypeMap sqlDbTypeMap) { IEnumerable dr = dt.Rows.Cast(); List dbList = new List(); DbColumn dbCoModel = null; foreach (var row in dr) { dbCoModel = new DbColumn(); if (row["ColumnID"] != null && row["ColumnID"].ToString() != "") { dbCoModel.ColumnID = int.Parse(row["ColumnID"].ToString()); } if (row["IsPrimaryKey"] != null && row["IsPrimaryKey"].ToString() != "") { dbCoModel.IsPrimaryKey = StrToBool(row["IsPrimaryKey"].ToString()); } if (row["ColumnName"] != null) { dbCoModel.ColumnName = row["ColumnName"].ToString(); } if (row["ColumnType"] != null) { dbCoModel.ColumnType = row["ColumnType"].ToString(); } if (row["IsIdentity"] != null && row["IsIdentity"].ToString() != "") { dbCoModel.IsIdentity = StrToBool(row["IsIdentity"].ToString()); } if (row["IsNullable"] != null && row["IsNullable"].ToString() != "") { dbCoModel.IsNullable = StrToBool(row["IsNullable"].ToString()); } if (row["ByteLength"] != null && row["ByteLength"].ToString() != "") { dbCoModel.ByteLength = int.Parse(row["ByteLength"].ToString()); } if (row["CharLength"] != null && row["CharLength"].ToString() != "") { dbCoModel.CharLength = int.Parse(row["CharLength"].ToString()); } if (row["Scale"] != null && row["Scale"].ToString() != "") { dbCoModel.Scale = int.Parse(row["Scale"].ToString()); } if (row["Remark"] != null) { dbCoModel.Remark = row["Remark"].ToString(); } dbCoModel.CSharpType = sqlDbTypeMap.MapCsharpType(dbCoModel.ColumnType); dbCoModel.IsNullCSharpType = sqlDbTypeMap.ChangSqlSqDbTypeMap(dbCoModel.CSharpType, dbCoModel.IsNullable); dbCoModel.CommonType = sqlDbTypeMap.MapCommonType(dbCoModel.ColumnType); dbList.Add(dbCoModel); } return dbList; } private bool StrToBool(string strBol) { string strbool = strBol.ToLower(); switch (strbool) { case "1": case "true": { return true; } case "0": case "false": { return false; } default: return false; } } } public interface ISqlServerDbTypeMap { string MapCsharpType(string dbtype); Type MapCommonType(string dbtype); string ChangSqlSqDbTypeMap(string csharp, bool isNullable); } #region DbTable /// /// 表结构 /// public sealed class DbTable { /// /// 表名称 /// public string TableName { get; set; } /// /// 表的架构 /// public string SchemaName { get; set; } /// /// 表的记录数 /// public int Rows { get; set; } /// /// 是否含有主键 /// public bool HasPrimaryKey { get; set; } } #endregion #region DbColumn /// /// 表字段结构 /// public sealed class DbColumn { /// /// 字段ID /// public int ColumnID { get; set; } /// /// 是否主键 /// public bool IsPrimaryKey { get; set; } /// /// 字段名称 /// public string ColumnName { get; set; } /// /// 字段类型 /// public string ColumnType { get; set; } /// /// 数据库类型对应的C#类型 /// public string CSharpType { get; set; } /// /// 数据库类型对应的C#类型 /// public string IsNullCSharpType { get; set; } /// /// 类型 /// public Type CommonType { get; set; } /// /// 字节长度 /// public int ByteLength { get; set; } /// /// 字符长度 /// public int CharLength { get; set; } /// /// 小数位 /// public int Scale { get; set; } /// /// 是否自增列 /// public bool IsIdentity { get; set; } /// /// 是否允许空 /// public bool IsNullable { get; set; } /// /// 描述 /// public string Remark { get; set; } } #endregion #>