/// <summary> /// 获取表列架构 /// </summary> /// <param name="tableName">表名</param> public static MDataColumn GetColumns(object tableName) { string dbName = StaticTool.GetDbName(ref tableName); string conn = string.Empty; string tName = tableName.ToString(); if (string.IsNullOrEmpty(dbName)) { conn = AppConfig.DB.DefaultConn; } else { conn = dbName + "Conn"; if (tName.IndexOfAny(new char[] { '(' }) == -1) { tName = SqlFormat.NotKeyword(tName); //tName = dbName + "." + tName;//单表 } } return(GetColumns(tName, conn)); }
/// <summary> /// 获取表列架构(链接错误时,抛异常) /// </summary> /// <param name="tableName">表名</param> /// <param name="conn">数据库链接</param> /// <param name="errInfo">出错时的错误信息</param> /// <returns></returns> public static MDataColumn GetColumns(string tableName, string conn, out string errInfo) { errInfo = string.Empty; DbBase helper = DalCreate.CreateDal(conn); DbResetResult result = helper.ChangeDatabaseWithCheck(tableName);//检测dbname.dbo.tablename的情况 switch (result) { case DbResetResult.No_DBNoExists: helper.Dispose(); return(null); case DbResetResult.No_SaveDbName: case DbResetResult.Yes: tableName = SqlFormat.NotKeyword(tableName); //same database no need database.tablename break; } if (!helper.TestConn(AllowConnLevel.MaterBackupSlave)) { errInfo = helper.debugInfo.ToString(); if (string.IsNullOrEmpty(errInfo)) { errInfo = "Open database fail : " + tableName; } helper.Dispose(); Error.Throw(errInfo); return(null); } if (!tableName.Contains(" "))// { tableName = GetMapTableName(conn, tableName); } MDataColumn mdc = TableSchema.GetColumns(tableName, ref helper); helper.Dispose(); return(mdc); }
/// <summary> /// 映射的表名 /// </summary> internal static string GetMapTableName(string conn, string tableName) { Dictionary <string, string> mapTable = null; string key = "MapTalbe:" + conn.GetHashCode(); if (Cache.CacheManage.LocalInstance.Contains(key)) { mapTable = Cache.CacheManage.LocalInstance.Get <Dictionary <string, string> >(key); } else { Dictionary <string, string> list = GetTables(conn); if (list != null && list.Count > 0) { mapTable = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); string mapName = string.Empty; foreach (string item in list.Keys) { mapName = item.Replace("_", "").Replace("-", "").Replace(" ", ""); //有奇葩用-符号和空格的。 //去掉原有的限制条件:(item != mapName 只存档带-," ",_等符号的表名 ),支持外部映射 if (!mapTable.ContainsKey(mapName)) // { mapTable.Add(mapName.ToLower(), item); } } Cache.CacheManage.LocalInstance.Set(key, mapTable, 1440); } } key = tableName.Replace("_", "").Replace("-", "").Replace(" ", "").ToLower(); key = SqlFormat.NotKeyword(key); if (mapTable != null && mapTable.Count > 0 && mapTable.ContainsKey(key)) { return(mapTable[key]); } return(tableName); }
/// <summary> /// 取消字段或表名添加关键字标签:如[],''等符号 /// </summary> /// <param name="name">表名或字段名</param> public static string NotKeyword(string name) { return(SqlFormat.NotKeyword(name)); }