private static DbBase GetDbBaseBy(ConnObject co) { string providerName = co.Master.ProviderName; //License.Check(providerName);//框架模块授权检测。 switch (providerName) { case SqlClient: return(new MsSqlDal(co)); case OleDb: return(new OleDbDal(co)); case OracleClient: return(new OracleDal(co)); case SQLiteClient: return(new SQLiteDal(co)); case MySqlClient: return(new MySQLDal(co)); case SybaseClient: return(new SybaseDal(co)); case TxtClient: case XmlClient: return(new NoSqlDal(co)); } return((DbBase)Error.Throw(string.Format("GetHelper:{0} No Be Support Now!", providerName))); }
public DbBase(ConnObject co) { this.connObject = co; this.conn = co.Master.Conn; this.providerName = co.Master.ProviderName; dalType = co.Master.ConnDalType; _fac = GetFactory(providerName); _con = _fac.CreateConnection(); _con.ConnectionString = DalCreate.FormatConn(dalType, conn); _com = _con.CreateCommand(); if (_com != null)//Txt| Xml 时返回Null { _com.Connection = _con; _com.CommandTimeout = AppConfig.DB.CommandTimeout; } if (IsAllowRecordSql)//开启秒表计算 { _watch = new Stopwatch(); } //if (AppConfig.DB.LockOnDbExe && dalType == DalType.Access) //{ // string dbName = DataBase; // if (!_dbOperator.ContainsKey(dbName)) // { // try // { // _dbOperator.Add(dbName, false); // } // catch // { // } // } //} //_com.CommandTimeout = 1; }
internal static ConnObject GetConnObject(string dbConn) { dbConn = string.IsNullOrEmpty(dbConn) ? AppConfig.DB.DefaultConn : dbConn; if (dbConn.EndsWith("_Bak")) { dbConn = dbConn.Replace("_Bak", ""); } if (connDicCache.ContainsKey(dbConn)) { return(connDicCache[dbConn]); } ConnBean cbMaster = GetConnBean(dbConn); if (cbMaster == null) { string errMsg = string.Format("Can't find the connection key '{0}' from web.config or app.config!", dbConn); if (dbConn == AppConfig.DB.DefaultConn) { Error.Throw(errMsg); } else { ConnBean cb = GetConnBean(AppConfig.DB.DefaultConn); if (cb != null) { cbMaster = cb.Clone();//获取默认的值。 } else { Error.Throw(errMsg); } } } ConnObject co = new ConnObject(); co.Master = cbMaster; if (dbConn != null && dbConn.Length < 32 && !dbConn.Trim().Contains(" ")) // 为configKey { ConnBean coBak = GetConnBean(dbConn + "_Bak"); if (coBak != null && coBak.ProviderName == cbMaster.ProviderName) { co.BackUp = coBak; } for (int i = 1; i < 1000; i++) { ConnBean cbSlave = GetConnBean(dbConn + "_Slave" + i); if (cbSlave == null) { break; } cbSlave.IsSlave = true; co.Slave.Add(cbSlave); } } if (!connDicCache.ContainsKey(dbConn) && co.Master.ConfigName == dbConn) // 非一致的,由外面切换后再缓存 { connDicCache.Set(dbConn, co); } return(co); }
/// <summary> /// 切换数据库(修改数据库链接) /// </summary> /// <param name="dbName"></param> /// <returns></returns> internal DbResetResult ChangeDatabase(string dbName) { if (_con.State == ConnectionState.Closed)//事务中。。不允许切换 { try { if (IsExistsDbNameWithCache(dbName))//新的数据库不存在。。不允许切换 { conn = GetNewConn(dbName); _con.ConnectionString = DalCreate.FormatConn(dalType, conn); connObject = DalCreate.GetConnObject(dbName + "Conn"); connObject.Master.Conn = conn; return(DbResetResult.Yes); } else { return(DbResetResult.No_DBNoExists); } } catch (Exception err) { Log.WriteLogToTxt(err); } } return(DbResetResult.No_Transationing); }
/// <summary> /// 切换数据库(修改数据库链接) /// </summary> internal DBResetResult ChangeDatabase(string dbName) { if (_con.State == ConnectionState.Closed)//事务中。。不允许切换 { try { if (IsExistsDbNameWithCache(dbName))//新的数据库不存在。。不允许切换 { string newConnString = GetConnString(dbName); _con.ConnectionString = newConnString; ConnObj = ConnObject.Create(dbName + "Conn"); ConnObj.Master.ConnName = dbName + "Conn"; ConnObj.Master.ConnString = newConnString; return(DBResetResult.Yes); } else { return(DBResetResult.No_DBNoExists); } } catch (Exception err) { Log.Write(err, LogType.DataBase); } } return(DBResetResult.No_Transationing); }
private static void fsy_Changed(object sender, FileSystemEventArgs e) { lock (o) { string json = JsonHelper.ReadJson(e.FullPath); Dictionary <string, string> dic = JsonHelper.Split(json); if (dic != null && dic.Count > 0) { foreach (KeyValuePair <string, string> item in dic) { //移除所有缓存的Key AppConfig.SetConn(item.Key, null); AppConfig.SetConn(item.Key + "_Bak", null); for (int i = 1; i < 10000; i++) { if (!AppConfig.SetConn(item.Key + "_Slave" + i, null)) { break; } } ConnObject.ClearCache(item.Key); } } } }
private static DalBase GetDalBaseBy(ConnObject co) { DataBaseType dalType = co.Master.ConnDalType; //License.Check(providerName);//框架模块授权检测。 switch (dalType) { case DataBaseType.MsSql: return(new MsSqlDal(co)); case DataBaseType.Access: return(new OleDbDal(co)); case DataBaseType.Oracle: return(new OracleDal(co)); case DataBaseType.SQLite: return(new SQLiteDal(co)); case DataBaseType.MySql: return(new MySQLDal(co)); case DataBaseType.Sybase: return(new SybaseDal(co)); case DataBaseType.PostgreSQL: return(new PostgreDal(co)); case DataBaseType.Txt: case DataBaseType.Xml: return(new NoSqlDal(co)); } return((DalBase)Error.Throw(string.Format("GetHelper:{0} No Be Support Now!", dalType.ToString()))); }
/// <summary> /// 测试链接 /// </summary> /// <param name="allowLevel">1:只允许当前主链接;2:允许主备链接;3:允许主备从链接</param> /// <returns></returns> internal bool TestConn(AllowConnLevel allowLevel) { threadCount = 0; openOKFlag = -1; ConnObject obj = connObject; if (obj.Master != null && obj.Master.IsOK && (int)allowLevel >= 1) { threadCount++; Thread thread = new Thread(new ParameterizedThreadStart(TestOpen)); thread.Start(obj.Master); } Thread.Sleep(30); if (openOKFlag == -1 && obj.BackUp != null && obj.BackUp.IsOK && (int)allowLevel >= 2) { threadCount++; Thread.Sleep(30); Thread thread = new Thread(new ParameterizedThreadStart(TestOpen)); thread.Start(obj.BackUp); } if (openOKFlag == -1 && obj.Slave != null && obj.Slave.Count > 0 && (int)allowLevel >= 3) { for (int i = 0; i < obj.Slave.Count; i++) { Thread.Sleep(30); if (openOKFlag == -1 && obj.Slave[i].IsOK) { threadCount++; Thread thread = new Thread(new ParameterizedThreadStart(TestOpen)); thread.Start(obj.Slave[i]); } } } int sleepTimes = 0; while (openOKFlag == -1) { sleepTimes += 30; if (sleepTimes > 3000 || threadCount == errorCount) { break; } Thread.Sleep(30); } return(openOKFlag == 1); }
//private const string SqlClient = "System.Data.SqlClient"; //private const string OleDb = "System.Data.OleDb"; //private const string OracleClient = "System.Data.OracleClient"; //private const string SQLiteClient = "System.Data.SQLite"; //private const string MySqlClient = "MySql.Data.MySqlClient"; //private const string SybaseClient = "Sybase.Data.AseClient"; //private const string PostgreClient = "System.Data.NpgSqlClient"; //private const string TxtClient = "CYQ.Data.TxtClient"; //private const string XmlClient = "CYQ.Data.XmlClient"; //private const string XHtmlClient = "CYQ.Data.XHtmlClient"; /// <summary> /// ¼òµ¥¹¤³§£¨Factory Method£© /// </summary> public static DalBase CreateDal(string connNameOrString) { //ABCConn DalBase db = GetDalBaseBy(ConnObject.Create(connNameOrString)); if (db.ConnObj.Master.ConnName != connNameOrString && connNameOrString.EndsWith("Conn"))//ÐèÒªÇл»ÅäÖᣠ{ //Conn A¿â //BConn xxx ÕÒ²»µ½Ê±£¬ÕÒĬÈÏ¿â¡£ DBResetResult result = db.ChangeDatabase(connNameOrString.Substring(0, connNameOrString.Length - 4)); if (result == DBResetResult.Yes) // дÈ뻺´æ { db.ConnObj.SaveToCache(connNameOrString); } } return(db); }
private static DalBase CreateDalBase(string connNameOrString) { //ABCConn DalBase db = GetDalBaseBy(ConnObject.Create(connNameOrString)); if (!string.IsNullOrEmpty(connNameOrString) && db.ConnObj.Master.ConnName.ToLower() != connNameOrString.ToLower() && connNameOrString.EndsWith("Conn"))//需要切换配置。 { //Conn A库 //BConn xxx 找不到时,找默认库。 DBResetResult result = db.ChangeDatabase(connNameOrString.Substring(0, connNameOrString.Length - 4)); if (result == DBResetResult.Yes) // 写入缓存 { db.ConnObj.SaveToCache(connNameOrString); } } return(db); }
/// <summary> /// 定时检测异常的链接是否恢复。 /// </summary> /// <param name="threadID"></param> public static void CheckConnIsOk(object threadID) { while (true) { Thread.Sleep(3000); if (connDicCache.Count > 0) { try { string[] items = new string[connDicCache.Count]; connDicCache.Keys.CopyTo(items, 0); foreach (string key in items) { ConnObject obj = connDicCache[key]; if (obj != null) { if (!obj.Master.IsOK) { obj.Master.TryTestConn(); } if (obj.BackUp != null && !obj.BackUp.IsOK) { obj.BackUp.TryTestConn(); } if (obj.Slave != null && obj.Slave.Count > 0) { for (int i = 0; i < obj.Slave.Count; i++) { if (!obj.Slave[i].IsOK) { obj.Slave[i].TryTestConn(); } } } } } items = null; } catch { } } } }
public DbBase(ConnObject co) { this.connObject = co; this.useConnBean = co.Master; this.conn = co.Master.ConnString; dalType = co.Master.ConnDalType; _fac = GetFactory(); _con = _fac.CreateConnection(); try { _con.ConnectionString = conn; } catch (Exception err) { Error.Throw("check the connectionstring is be ok!" + AppConst.BR + "error:" + err.Message + AppConst.BR + conn); } _com = _con.CreateCommand(); if (_com != null)//Txt| Xml 时返回Null { _com.Connection = _con; _com.CommandTimeout = AppConfig.DB.CommandTimeout; } if (IsAllowRecordSql)//开启秒表计算 { _watch = new Stopwatch(); } //if (AppConfig.DB.LockOnDbExe && dalType == DalType.Access) //{ // string dbName = DataBase; // if (!_dbOperator.ContainsKey(dbName)) // { // try // { // _dbOperator.Add(dbName, false); // } // catch // { // } // } //} //_com.CommandTimeout = 1; }
public OracleDal(ConnObject co) : base(co) { }
private CacheManage _Cache = CacheManage.LocalInstance; //Cache操作 #endregion Fields #region Constructors public MySQLDal(ConnObject co) : base(co) { }
public OleDbDal(ConnObject co) : base(co) { }
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作 public DB2Dal(ConnObject co) : base(co) { }
public NoSqlDal(ConnObject co) : base(co) { }
public MsSqlDal(ConnObject co) : base(co) { }
private CacheManage _Cache = CacheManage.LocalInstance; //Cache操作 #endregion Fields #region Constructors public SQLiteDal(ConnObject co) : base(co) { base.isUseUnsafeModeOnSqlite = co.Master.Conn.ToLower().Contains("syncpragma=off"); }
/// <summary> /// 切换数据库(修改数据库链接) /// </summary> /// <param name="dbName"></param> /// <returns></returns> internal DbResetResult ChangeDatabase(string dbName) { if (_con.State == ConnectionState.Closed)//事务中。。不允许切换 { try { if (IsExistsDbNameWithCache(dbName))//新的数据库不存在。。不允许切换 { conn = GetNewConn(dbName); _con.ConnectionString = DalCreate.FormatConn(dalType, conn); connObject = DalCreate.GetConnObject(dbName + "Conn"); connObject.Master.Conn = conn; return DbResetResult.Yes; } else { return DbResetResult.No_DBNoExists; } } catch (Exception err) { Log.WriteLogToTxt(err); } } return DbResetResult.No_Transationing; }
public SybaseDal(ConnObject co) : base(co) { }
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作 public MySQLDal(ConnObject co) : base(co) { }
public PostgreDal(ConnObject co) : base(co) { }
internal static ConnObject GetConnObject(string dbConn) { dbConn = string.IsNullOrEmpty(dbConn) ? AppConfig.DB.DefaultConn : dbConn; if (connDicCache.ContainsKey(dbConn)) { return connDicCache[dbConn]; } ConnBean cbMaster = GetConnBean(dbConn); if (cbMaster == null) { if (dbConn == AppConfig.DB.DefaultConn) { Error.Throw(string.Format("Can't find the connection key '{0}' from web.config or app.config!", dbConn)); } else { ConnBean cb = GetConnBean(AppConfig.DB.DefaultConn); cbMaster = cb.Clone();//��ȡĬ�ϵ�ֵ�� } } ConnObject co = new ConnObject(); co.Master = cbMaster; if (dbConn != null && dbConn.Length < 32 && !dbConn.Trim().Contains(" ")) // ΪconfigKey { ConnBean coBak = GetConnBean(dbConn + "_Bak"); if (coBak != null && coBak.ProviderName == cbMaster.ProviderName) { co.BackUp = coBak; } for (int i = 1; i < 1000; i++) { ConnBean cbSlave = GetConnBean(dbConn + "_Slave" + i); if (cbSlave == null) { break; } co.Slave.Add(cbSlave); } } if (!connDicCache.ContainsKey(dbConn)) { connDicCache.Add(dbConn, co); } return co; }
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作 public SQLiteDal(ConnObject co) : base(co) { base.isUseUnsafeModeOnSqlite = co.Master.Conn.ToLower().Contains("syncpragma=off"); }
private static DbBase GetDbBaseBy(ConnObject co) { string providerName = co.Master.ProviderName; //License.Check(providerName);//���ģ����Ȩ��⡣ switch (providerName) { case SqlClient: return new MsSqlDal(co); case OleDb: return new OleDbDal(co); case OracleClient: return new OracleDal(co); case SQLiteClient: return new SQLiteDal(co); case MySqlClient: return new MySQLDal(co); case SybaseClient: return new SybaseDal(co); case TxtClient: case XmlClient: return new NoSqlDal(co); } return (DbBase)Error.Throw(string.Format("GetHelper:{0} No Be Support Now!", providerName)); }