internal static DalType GetDalType(string tableName) { string dbName = GetDBName(tableName); if (!string.IsNullOrEmpty(dbName) && _DbTypeDic.ContainsKey(dbName)) { return(_DbTypeDic[dbName]); } return(DBTool.GetDalType(AppConfig.DB.DefaultConn)); }
/// <summary> /// 初始化状态[继承此基类的实体在构造函数中需调用此方法] /// </summary> /// <param name="entityInstance">实体对象,一般写:this</param> /// <param name="tableName">表名,如:Users</param> /// <param name="conn">数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串</param> protected void SetInit(Object entityInstance, string tableName, string conn) { conn = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn; entity = entityInstance; typeInfo = entity.GetType(); try { if (string.IsNullOrEmpty(tableName)) { tableName = typeInfo.Name; if (tableName.EndsWith(AppConfig.EntitySuffix)) { tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length); } } string key = tableName + MD5.Get(conn); if (!CacheManage.LocalInstance.Contains(key)) { DalType dal = DBTool.GetDalType(conn); bool isTxtDal = dal == DalType.Txt || dal == DalType.Xml; string errMsg = string.Empty; Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。 if (Columns == null || Columns.Count == 0) { if (errMsg != string.Empty) { Error.Throw(errMsg); } Columns = TableSchema.GetColumns(typeInfo); if (!DBTool.ExistsTable(tableName, conn)) { if (!DBTool.CreateTable(tableName, Columns, conn)) { Error.Throw("Create Table Error:" + tableName); } } } else if (isTxtDal)//文本数据库 { if (FieldSource != FieldSource.Data) { MDataColumn c2 = TableSchema.GetColumns(typeInfo); if (FieldSource == FieldSource.BothOfAll) { Columns.AddRange(c2); } else { Columns = c2; } } } if (Columns != null && Columns.Count > 0) { CacheManage.LocalInstance.Add(key, Columns, null, 1440); } } else { Columns = CacheManage.LocalInstance.Get(key) as MDataColumn; } action = new MAction(Columns.ToRow(tableName), conn); if (typeInfo.Name == "SysLogs") { action.SetAopOff(); } action.EndTransation(); } catch (Exception err) { if (typeInfo.Name != "SysLogs") { Log.WriteLogToTxt(err); } throw; } }
static void Start() { bool result = DBTool.TestConn(AppConfig.DB.DefaultConn);//检测数据库链接是否正常 OutMsg("数据库链接:" + result); OutMsg("-----------------------------------------"); string databaseName; Dictionary <string, string> tables = DBTool.GetTables(AppConfig.DB.DefaultConn, out databaseName);//读取所有表 if (tables != null) { OutMsg("数据库:" + databaseName); foreach (KeyValuePair <string, string> item in tables) { OutMsg("表:" + item.Key + " 说明:" + item.Value); MDataColumn mdc = DBTool.GetColumns(item.Key);//读取所有列 foreach (MCellStruct ms in mdc) { OutMsg(" 列:" + ms.ColumnName + " SqlType:" + ms.SqlType); } } } OutMsg("-----------------------------------------"); string newTableName = "A18";// +DateTime.Now.Second; DalType dalType; result = DBTool.ExistsTable(newTableName, AppConfig.DB.DefaultConn, out dalType);//检测表是否存在 OutMsg("表 " + newTableName + (result ? "存在" : "不存在") + " 数据库类型:" + dalType); OutMsg("-----------------------------------------"); if (result) { result = DBTool.DropTable(newTableName); OutMsg("表 " + newTableName + " 删除?" + result); OutMsg("-----------------------------------------"); } MDataColumn newMdc = new MDataColumn(); newMdc.Add("ID", System.Data.SqlDbType.Int); newMdc.Add("Name", System.Data.SqlDbType.NVarChar); result = DBTool.CreateTable(newTableName, newMdc); OutMsg("表 " + newTableName + " 创建?" + result); OutMsg("-----------------------------------------"); newMdc[1].ColumnName = "UserName"; newMdc[1].AlterOp = AlterOp.Rename; //将新创建的表name => username newMdc.Add("Password"); newMdc[2].AlterOp = AlterOp.AddOrModify; // 新增列 Password result = DBTool.AlterTable(newTableName, newMdc); OutMsg("表 " + newTableName + " 修改结构?" + result); OutMsg("-----------------------------------------"); OutMsg("------------------其它操作-------------------"); dalType = DBTool.GetDalType("txt path={0}"); OutMsg("数据库类型为: " + dalType); OutMsg("-----------------------------------------"); OutMsg(DBTool.Keyword("表关键字", DalType.MsSql));//DBTool.NotKeyword 则取消 OutMsg(DBTool.Keyword("表关键字", DalType.Oracle)); OutMsg(DBTool.Keyword("表关键字", DalType.MySql)); OutMsg(DBTool.Keyword("表关键字", DalType.SQLite)); string changeDataType = DBTool.GetDataType(newMdc[0], DalType.Access, string.Empty); OutMsg("数据类型为: " + changeDataType); OutMsg("-----------------------------------------"); string formatValue = DBTool.FormatDefaultValue(DalType.Access, "[#GETDATE]", 1, System.Data.SqlDbType.DateTime); OutMsg("Access的日期数据类型为: " + formatValue); OutMsg("-----------------------------------------"); }
/// <summary> /// 将原有的初始化改造成延时加载。 /// </summary> private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op) { if (string.IsNullOrEmpty(conn)) { //不设置链接,则忽略(当成普通的实体类) return; } entity = entityInstance; typeInfo = entity.GetType(); try { if (string.IsNullOrEmpty(tableName)) { tableName = typeInfo.Name; if (tableName.EndsWith(AppConfig.EntitySuffix)) { tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length); } } string key = tableName + StaticTool.GetHashKey(conn); if (!CacheManage.LocalInstance.Contains(key)) { DalType dal = DBTool.GetDalType(conn); bool isTxtDal = dal == DalType.Txt || dal == DalType.Xml; string errMsg = string.Empty; Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。 if (Columns == null || Columns.Count == 0) { if (errMsg != string.Empty) { Error.Throw(errMsg); } Columns = ColumnSchema.GetColumns(typeInfo); ConnBean connBean = ConnBean.Create(conn);//下面指定链接,才不会在主从备时被切换到其它库。 if (!DBTool.ExistsTable(tableName, connBean.ConnString)) { DBTool.ErrorMsg = null; if (!DBTool.CreateTable(tableName, Columns, connBean.ConnString)) { Error.Throw("SimpleOrmBase :Create Table " + tableName + " Error:" + DBTool.ErrorMsg); } } } else if (isTxtDal)//文本数据库 { if (FieldSource != FieldSource.Data) { MDataColumn c2 = ColumnSchema.GetColumns(typeInfo); if (FieldSource == FieldSource.BothOfAll) { Columns.AddRange(c2); } else { Columns = c2; } } } if (Columns != null && Columns.Count > 0) { CacheManage.LocalInstance.Set(key, Columns, 1440, null); } } else { Columns = CacheManage.LocalInstance.Get(key) as MDataColumn; } _Action = new MAction(Columns.ToRow(tableName), conn); if (typeInfo.Name == "SysLogs") { _Action.SetAopState(Aop.AopOp.CloseAll); } else { _Action.SetAopState(op); } _Action.EndTransation(); } catch (Exception err) { if (typeInfo.Name != "SysLogs") { Log.Write(err, LogType.DataBase); } throw; } }