/// <summary> /// 移除一张表 /// <param name="conn">数据库链接</param> /// </summary> public static bool DropTable(string tableName, string conn) { bool result = false; string key = string.Empty; using (DbBase helper = DalCreate.CreateDal(conn)) { key = TableSchema.GetTableCacheKey(helper); DalType dalType = helper.dalType; switch (dalType) { case DalType.Txt: case DalType.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 == DalType.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, helper.DataBase, dalType); } break; } if (helper.recordsAffected == -2) { _ErrorMsg.AppendLine(helper.debugInfo.ToString()); } } if (result) { //处理数据库表字典缓存 if (TableSchema.tableCache.ContainsKey(key)) { Dictionary <string, string> tableDic = TableSchema.tableCache[key]; if (tableDic.ContainsKey(tableName)) { tableDic.Remove(tableName); } } } return(result); }
/// <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; DalType dalType = GetDalType(conn); string dataBase = string.Empty; switch (dalType) { case DalType.Txt: case DalType.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")) //不写入表架构。 { return(true); } else { tableName = Path.GetFileNameWithoutExtension(tableName); string fileName = NoSqlConnection.GetFilePath(conn) + tableName + ".ts"; result = columns.WriteSchema(fileName); dataBase = new NoSqlConnection(conn).Database; } break; default: using (MProc proc = new MProc(null, conn)) { dataBase = proc.DataBase; try { proc.dalHelper.IsAllowRecordSql = false; proc.SetAopState(Aop.AopOp.CloseAll); proc.ResetProc(GetCreateTableSql(tableName, columns, proc.DalType, proc.DalVersion)); //.Replace("\n", string.Empty) result = proc.ExeNonQuery() > -2; //获取扩展说明 string descriptionSql = GetCreateTableDescriptionSql(tableName, columns, proc.DalType).Replace("\r\n", " ").Trim(' ', ';'); if (!string.IsNullOrEmpty(descriptionSql)) { if (proc.DalType == DalType.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.WriteLogToTxt(err); } } break; } if (result) { //处理表缓存 string key = TableSchema.GetTableCacheKey(dalType, dataBase, conn); if (TableSchema.tableCache.ContainsKey(key)) { Dictionary <string, string> tableDic = TableSchema.tableCache[key]; if (!tableDic.ContainsKey(tableName)) { tableDic.Add(tableName, ""); } } } return(result); }