/// <summary> /// 创建SQL实体文件 /// </summary> public void CreateClassFiles(SqlSugarClient db, string fileDirectory, string nameSpace = null) { var tables = db.GetDataTable("select name from sysobjects where xtype='U'"); if (tables != null && tables.Rows.Count > 0) { foreach (DataRow dr in tables.Rows) { string tableName = dr["name"].ToString(); var currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName)); var classCode = DataTableToClass(currentTable, tableName, nameSpace); FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode); } } }
/// <summary> /// 创建SQL实体文件,指定表名 /// </summary> public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames) { string sql = GetCreateClassSql(null); var tables = db.GetDataTable(sql); if (tables != null && tables.Rows.Count > 0) { foreach (DataRow dr in tables.Rows) { string tableName = dr["name"].ToString().ToLower(); if (tableNames.Any(it => it.ToLower() == tableName)) { var currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName)); var classCode = DataTableToClass(currentTable, tableName, nameSpace); FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode); } } } }
/// <summary> /// 创建SQL实体文件 /// </summary> /// <param name="db"></param> /// <param name="fileDirectory"></param> /// <param name="nameSpace">命名空间(默认:null)</param> /// <param name="tableOrView">是生成视图文件还是表文件,null生成表和视图,true生成表,false生成视图(默认为:null)</param> public void CreateClassFiles(SqlSugarClient db, string fileDirectory, string nameSpace = null, bool?tableOrView = null, Action <string> callBack = null) { var tables = db.GetDataTable("select name from sysobjects where xtype in ('U','V') "); if (tableOrView != null) { if (tableOrView == true) { tables = db.GetDataTable("select name from sysobjects where xtype in ('U') "); } else { tables = db.GetDataTable("select name from sysobjects where xtype in ('V') "); } } if (tables != null && tables.Rows.Count > 0) { foreach (DataRow dr in tables.Rows) { string tableName = dr["name"].ToString(); var currentTable = db.GetDataTable(string.Format("select top 1 * from {0}", tableName)); if (callBack != null) { var tableColumns = GetTableColumns(db, tableName); var classCode = DataTableToClass(currentTable, tableName, nameSpace, tableColumns); string className = db.GetClassTypeByTableName(tableName); classCode = classCode.Replace("class " + tableName, "class " + className); FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode); callBack(className); } else { var classCode = DataTableToClass(currentTable, tableName, nameSpace); FileSugar.WriteText(fileDirectory.TrimEnd('\\') + "\\" + tableName + ".cs", classCode); } } } }