Пример #1
0
        public bool TryTestConn()
        {
            //err = string.Empty;
            if (!string.IsNullOrEmpty(ConnName))
            {
                DalBase helper = DalCreate.CreateDal(ConnName);
                try
                {

                    helper.Con.Open();
                    Version = helper.Con.ServerVersion;
                    if (string.IsNullOrEmpty(Version)) { Version = helper.DataBaseType.ToString(); }
                    helper.Con.Close();
                    IsOK = true;
                    ErrorMsg = string.Empty;
                }
                catch (Exception er)
                {
                    ErrorMsg = er.Message;
                    IsOK = false;
                }
                finally
                {
                    helper.Dispose();
                }
            }
            else
            {
                IsOK = false;
            }
            return IsOK;
        }
Пример #2
0
 /// <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);
 }
Пример #3
0
 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;
 }
Пример #4
0
 private void Init(object procNameOrSql, string conn)
 {
     #region 分析是Sql或者存储过程
     if (procNameOrSql != null)
     {
         if (procNameOrSql is Enum)
         {
             Type   t        = procNameOrSql.GetType();
             string enumName = t.Name;
             if (enumName != "ProcNames")
             {
                 if (enumName.Length > 1 && enumName[1] == '_')
                 {
                     conn = enumName.Substring(2).Replace("Enum", "Conn");
                 }
                 else
                 {
                     string[] items = t.FullName.Split('.');
                     if (items.Length > 1)
                     {
                         conn  = items[items.Length - 2] + "Conn";
                         items = null;
                     }
                 }
             }
             t = null;
         }
         _procName = procNameOrSql.ToString().Trim();
         _isProc   = _procName.IndexOf(' ') == -1;//不包含空格
     }
     #endregion
     _conn = conn;
     SetDbBase(DalCreate.CreateDal(conn));
 }
Пример #5
0
 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);
 }
Пример #6
0
 /// <summary>
 /// 切换链接
 /// </summary>
 /// <param name="cb"></param>
 private void ResetConn(ConnBean cb)
 {
     if (_con != null && _con.State != ConnectionState.Open && conn != cb.Conn && _IsAllowRecordSql)
     {
         conn = cb.Conn;//切换。
         _con.ConnectionString = DalCreate.FormatConn(dalType, conn);
     }
 }
Пример #7
0
            private static void SetDefault()
            {
                DbBase db = DalCreate.CreateDal(DefaultConn);

                if (db != null)
                {
                    _DefaultDataBase = db.DataBase;
                    _DefaultDalType  = db.dalType;
                    db.Dispose();
                }
            }
Пример #8
0
 /// <summary>
 /// 切换链接
 /// </summary>
 /// <param name="cb"></param>
 private bool ResetConn(ConnBean cb)//, bool isAllowReset
 {
     if (cb != null && cb.IsOK && _con != null && _con.State != ConnectionState.Open && conn != cb.Conn)
     {
         useConnBean           = cb;
         conn                  = cb.Conn;//切换。
         _con.ConnectionString = DalCreate.FormatConn(dalType, conn);
         return(true);
     }
     return(false);
 }
Пример #9
0
 internal DalBase ResetDalBase(string dbTableName)
 {
     if (IsOwnerOtherDb(dbTableName))              //是其它数据库名称。
     {
         if (_con.State != ConnectionState.Closed) //事务中。。创建新链接切换
         {
             string dbName = dbTableName.Split('.')[0];
             return(DalCreate.CreateDal(GetConnString(dbName)));
         }
     }
     return(this);
 }
Пример #10
0
        internal DbBase ResetDbBase(string dbTableName)
        {
            if (IsOwnerOtherDb(dbTableName))//是其它数据库名称。
            {
                string dbName = dbTableName.Split('.')[0];
                if (_con.State != ConnectionState.Closed)//事务中。。创建新链接切换
                {
                    return DalCreate.CreateDal(GetNewConn(dbName));
                }

            }
            return this;
        }
Пример #11
0
        //public string TryTestConn()
        //{
        //    string err;
        //    return TryTestConn(out err);
        //}
        public string TryTestConn()
        {
            string version = string.Empty;

            //err = string.Empty;
            if (!string.IsNullOrEmpty(Conn))
            {
                DbBase helper = DalCreate.CreateDal(Conn);
                try
                {
                    helper.Con.Open();
                    version = helper.Con.ServerVersion;
                    if (string.IsNullOrEmpty(version))
                    {
                        version = helper.dalType.ToString();
                    }
                    helper.Con.Close();
                    IsOK     = true;
                    ErrorMsg = string.Empty;
                }
                catch (Exception er)
                {
                    ErrorMsg = er.Message;
                    //err = er.Message;
                    IsOK = false;
                }
                finally
                {
                    helper.Dispose();
                }
            }
            else
            {
                IsOK = false;
            }
            return(version);
        }