private void Init(object procNameOrSql, string conn, bool isClearPara) { #region 分析是Sql或者存储过程 if (procNameOrSql != null) { if (string.IsNullOrEmpty(conn)) { if (procNameOrSql is Enum) { conn = CrossDB.GetConnByEnum(procNameOrSql as Enum); } else if (procNameOrSql is String) { string fixName; conn = CrossDB.GetConn(procNameOrSql.ToString(), out fixName); } } _procName = procNameOrSql.ToString().Trim(); _isProc = _procName.IndexOf(' ') == -1;//不包含空格 } #endregion DalBase dalBase = null; if (conn != null && dalHelper == null) { dalBase = DalCreate.CreateDal(conn); } SetDalBase(dalBase, isClearPara); }
internal static string GetBaseKey(string tableName, string conn) { if (string.IsNullOrEmpty(conn)) { conn = CrossDB.GetConn(tableName, out tableName, conn); } return("AutoCache:" + ConnBean.GetHashKey(conn) + "." + tableName); }
/// <summary> /// 获取表列架构 /// </summary> /// <param name="tableName">表名</param> public static MDataColumn GetColumns(object tableNameObj) { string conn = string.Empty; if (tableNameObj is Enum) { conn = CrossDB.GetConnByEnum(tableNameObj as Enum); } return(GetColumns(Convert.ToString(tableNameObj), conn)); }
/// <summary> /// 移除一张表 /// <param name="conn">数据库链接</param> /// </summary> public static bool DropTable(string tableName, string conn) { bool result = false; string key = string.Empty; using (DalBase helper = DalCreate.CreateDal(conn)) { DataBaseType dalType = helper.DataBaseType; switch (dalType) { case DataBaseType.Txt: case DataBaseType.Xml: string folder = helper.Con.DataSource + Path.GetFileNameWithoutExtension(tableName); string path = folder + ".ts"; try { if (File.Exists(path)) { result = IOHelper.Delete(path); } path = folder + (dalType == DataBaseType.Txt ? ".txt" : ".xml"); if (File.Exists(path)) { result = IOHelper.Delete(path); } } catch { } break; default: result = helper.ExeNonQuery("drop table " + Keyword(tableName, dalType), false) != -2; if (result) { //处理表相关的元数据和数据缓存。 RemoveCache(tableName, conn); } break; } if (helper.RecordsAffected == -2) { _ErrorMsg.AppendLine(helper.DebugInfo.ToString()); } } if (result) { //处理数据库表字典缓存 CrossDB.Remove(tableName, "U", conn); } return(result); }
internal static string GetTableName(Type t, out string conn) { conn = string.Empty; string[] items = t.FullName.Split('.'); if (items.Length > 1) { conn = items[items.Length - 2] + "Conn"; if (string.IsNullOrEmpty(AppConfig.GetConn(conn))) { conn = null; } items = null; } string tName = t.Name; object[] names = ReflectTool.GetAttributes(t, typeof(TableNameAttribute)); if (names != null && names.Length > 0) { foreach (object item in names) { if (item is TableNameAttribute) { tName = ((TableNameAttribute)item).TableName; break; } } } if (!string.IsNullOrEmpty(tName)) { t = null; if (tName.EndsWith(AppConfig.EntitySuffix)) { tName = tName.Substring(0, tName.Length - AppConfig.EntitySuffix.Length); } } string fixName; conn = CrossDB.GetConn(tName, out fixName, conn); if (!string.IsNullOrEmpty(fixName)) { tName = fixName; } return(tName); }
private static string GetTableName <T>(out string conn) { conn = string.Empty; Type t = typeof(T); string[] items = t.FullName.Split('.'); if (items.Length > 1) { conn = items[items.Length - 2] + "Conn"; items = null; } string tName = t.Name; t = null; if (tName.EndsWith(AppConfig.EntitySuffix)) { tName = tName.Substring(0, tName.Length - AppConfig.EntitySuffix.Length); } tName = CrossDB.GetFixName(tName, conn); return(tName); }
/// <summary> /// 为指定的表架构生成SQL(Create Table)语句 /// </summary> public static bool CreateTable(string tableName, MDataColumn columns, string conn) { if (string.IsNullOrEmpty(tableName) || tableName.Contains("(") && tableName.Contains(")")) { return(false); } bool result = false; DataBaseType dalType = GetDataBaseType(conn); string dataBase = string.Empty; switch (dalType) { case DataBaseType.Txt: case DataBaseType.Xml: // string a, b, c; conn = AppConfig.GetConn(conn); // CYQ.Data.DAL.DalCreate.GetConnString(conn, out a, out b, out c); if (conn.ToLower().Contains(";ts=0")) //不写入表架构。 { //增加缓存 result = true; } else { tableName = Path.GetFileNameWithoutExtension(tableName); string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts"; result = columns.WriteSchema(fileName); dataBase = GetDBInfo(conn).DataBaseName; } break; default: #region MyRegion using (MProc proc = new MProc(null, conn)) { dataBase = proc.DataBaseName; try { proc.dalHelper.IsRecordDebugInfo = false; proc.SetAopState(Aop.AopOp.CloseAll); proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DataBaseType, proc.DataBaseVersion)); //.Replace("\n", string.Empty) result = proc.ExeNonQuery() > -2; if (result) { //获取扩展说明 string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DataBaseType).Replace("\r\n", " ").Trim(' ', ';'); if (!string.IsNullOrEmpty(descriptionSql)) { if (proc.DataBaseType == DataBaseType.Oracle) { foreach (string sql in descriptionSql.Split(';')) { proc.ResetProc(sql); if (proc.ExeNonQuery() == -2) { break; } } } else { proc.ResetProc(descriptionSql); proc.ExeNonQuery(); } } } } catch (Exception err) { Log.Write(err, LogType.DataBase); } finally { if (proc.RecordsAffected == -2) { _ErrorMsg.AppendLine("CreateTable:" + proc.DebugInfo); } } } #endregion break; } if (result) { CrossDB.Add(tableName, "U", conn);//修改缓存。 } return(result); }
public static bool Exists(string name, string type, string conn) { return(CrossDB.Exists(name, type, conn)); }