public HttpResponseMessage List() { ReturnData result = new ReturnData(); try { //TODO:ConnectionString会传到前端,需要修正 //Dictionary<string, string> dicDBList = new Dictionary<string, string>(); //List<KeyValueData> li = new List<KeyValueData>(); DataCache cache = new DataCache(); object DBList = cache.GetCache("Sys_DBList2"); if (DBList == null) { result.D = DBInfoCache.GetDBListWithoutConStr(); } else { result.D = DBList; } result.S = true; } catch (Exception ex) { result.S = false; result.SetErrorMsg("获取数据库信息失败:" + ex.Message); } return(MyPlatform.Utils.MyResponseMessage.SuccessJson <ReturnData>(result)); }
/// <summary> /// 是否存在表,0-不存在,1-存在 /// </summary> /// <param name="tableName">表名</param> /// <param name="dbName">数据库名</param> /// <param name="dbTypeCode">数据库类型</param> /// <returns></returns> public ReturnData ExistsTable(string tableName, string dbCon) { ReturnData result = new ReturnData(); try { IDataBase db = DBHelperFactory.CreateDBInstance(dbCon); Dictionary <string, string> dic = DBInfoCache.GetDBInfo(dbCon); if (dic == null) { result.SetErrorMsg("找不到对应的数据库配置信息"); } else { string sql = ""; IDataParameter[] paras = new IDataParameter[1]; switch (dic["DBTypeCode"].ToLower()) { case "sqlserver": sql = " SELECT 1 FROM dbo.SysObjects WHERE ID = object_id(@tableName) AND OBJECTPROPERTY(ID, 'IsTable') = 1 "; paras = new IDataParameter[1] { new SqlParameter("@tableName", SqlDbType.VarChar, 30) }; paras[0].Value = tableName; break; case "oracle": //TODO:集成oracle result.SetErrorMsg("oracle数据库未实现!"); return(result); break; case "mysql": //TODO:集成mysql result.SetErrorMsg("oracle数据库未实现!"); return(result); break; default: break; } result.S = Convert.ToInt32(db.ExecuteScalar(sql, paras)) > 0; if (!result.S) { result.M = "数据库已经存在表名为:" + tableName + "的表"; } result.S = true; } } catch (SqlException ex) { result.SetErrorMsg(ex.Message); } return(result); }
/// <summary> /// 根据数据库连接名,返回对应DBHelper实例 /// </summary> /// <param name="conName"></param> /// <returns></returns> public static IDataBase CreateDBInstance(string conName) { IDataBase db; DataCache cache = new DataCache(); object DBList = cache.GetCache("Sys_DBList"); string dbType = ""; if (DBList == null) { DBList = DBInfoCache.GetDBList(); } List <Dictionary <string, string> > li = DBList as List <Dictionary <string, string> >; foreach (Dictionary <string, string> dic in li) { if (dic["DBCon"].ToString().ToLower() == conName.ToLower()) { dbType = dic["DBTypeCode"].ToString().ToLower(); break; } } if (string.IsNullOrEmpty(dbType)) { throw new Exception("不能打开数据,数据库连接为空!"); } switch (dbType.ToLower()) { case "sqlserver": db = new SqlServerDataBase(conName); break; case "oracle": db = new OracleDataBase(conName); break; case "mysql": throw new Exception("系统暂不支持MySql数据库"); break; default: db = new SqlServerDataBase(); break; } return(db); }
/// <summary> /// 根据dbCon返回DB所有信息 /// </summary> /// <param name="dbCon"></param> /// <returns></returns> public Dictionary <string, string> GetDBInfo(string dbCon) { DataCache cache = new DataCache(); object DBList = cache.GetCache("Sys_DBList"); if (DBList == null) { DBList = DBInfoCache.GetDBList(); } List <Dictionary <string, string> > li = DBList as List <Dictionary <string, string> >; Dictionary <string, string> s = new Dictionary <string, string>(); foreach (Dictionary <string, string> dic in li) { if (dic["DBCon"].ToString().ToLower() == dbCon.ToLower()) { return(dic); } } return(null); }