Esempio n. 1
0
 /// <summary>
 ///     关闭连接
 /// </summary>
 public void Close()
 {
     if (_connection == null)
     {
         return;
     }
     try
     {
         lock (LockData)
         {
             if (_connection.State == ConnectionState.Open)
             {
                 //Trace.WriteLine("Close Connection", "MySqlDataBase");
                 _connection.Close();
             }
             Connections.Remove(_connection);
             LogRecorder.MonitorTrace($"未关闭总数{Connections.Count}");
             _connection = null;
         }
     }
     catch (Exception exception)
     {
         _connection?.Dispose();
         Debug.WriteLine("Close Error", "MySqlDataBase");
         LogRecorder.Error(exception.ToString());
     }
     finally
     {
         if (_default == this)
         {
             _default = null;
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        ///     插入数据
        /// </summary>
        /// <param name="entity">插入数据的实体</param>
        private bool UpdateInner(TData entity)
        {
            if (UpdateByMidified && !entity.__EntityStatus.IsModified)
            {
                return(false);
            }
            int result;

            PrepareSave(entity, DataOperatorType.Update);
            using (var cmd = DataBase.CreateCommand())
            {
                SetUpdateCommand(entity, cmd);
                MySqlDataBase.TraceSql(cmd);
                cmd.CommandText = $@"{BeforeUpdateSql(PrimaryKeyConditionSQL)}
{UpdateSqlCode}
{AfterUpdateSql(PrimaryKeyConditionSQL)}";

                result = cmd.ExecuteNonQuery();
            }

            if (result <= 0)
            {
                return(false);
            }
            EndSaved(entity, DataOperatorType.Update);
            return(true);
        }
Esempio n. 3
0
 /// <summary>
 ///     更新数据
 /// </summary>
 /// <param name="entity">更新数据的实体</param>
 protected bool InsertInner(TData entity)
 {
     PrepareSave(entity, DataOperatorType.Insert);
     using (var cmd = DataBase.CreateCommand())
     {
         var isIdentitySql = SetInsertCommand(entity, cmd);
         MySqlDataBase.TraceSql(cmd);
         if (isIdentitySql)
         {
             var key = cmd.ExecuteScalar();
             if (key == DBNull.Value || key == null)
             {
                 return(false);
             }
             entity.SetValue(KeyField, key);
         }
         else
         {
             if (cmd.ExecuteNonQuery() == 0)
             {
                 return(false);
             }
         }
     }
     EndSaved(entity, DataOperatorType.Insert);
     return(true);
 }
        /// <summary>
        ///     AddParameter
        /// </summary>
        /// <param name="value">参数值</param>
        /// <returns>参数名称</returns>
        public string AddParameter(object value)
        {
            var name = NewParameterName;

            _parameters.Add(name, MySqlDataBase.CreateParameter(name, value));
            return(name);
        }
 /// <summary>
 ///     构造
 /// </summary>
 /// <param name="dataBase"></param>
 protected MySqlDataBaseScope(MySqlDataBase dataBase)
 {
     //Trace.WriteLine("Create MySqlDataBaseScope", "MySqlDataBase");
     _dataBase = dataBase;
     MySqlDataBase.DefaultDataBase = dataBase;
     if (dataBase.Open())
     {
         _isHereOpen = true;
     }
     dataBase.QuoteCount += 1;
 }
Esempio n. 6
0
        /// <summary>
        ///     构造
        /// </summary>
        /// <param name="dataBase">数据库对象</param>
        private TransactionScope(MySqlDataBase dataBase)
        {
            dbScope      = MySqlDataBaseScope.CreateScope(dataBase);
            _preScope    = CurrentScope;
            CurrentScope = this;

            _dataBase = dataBase;
            dataBase.Open();
            if (dataBase.Transaction != null)
            {
                return;
            }
            _beginType           = 1;
            dataBase.Transaction = dataBase.Connection.BeginTransaction();
        }
Esempio n. 7
0
        /*// <summary>
         * ///     数据库连接对象
         * /// </summary>
         * //private readonly MySqlDataBase _dataBase;
         *
         * /// <summary>
         * ///     在范围之前的连接对象
         * /// </summary>
         * private readonly SqlConnection _oldConnection;
         *
         * /// <summary>
         * ///     在范围之前的事务对象
         * /// </summary>
         * private readonly SqlTransaction _oldTransaction;
         *
         * /// <summary>
         * ///     上一个范围
         * /// </summary>
         * //private readonly TransactionScope _preScope;
         *
         * /// <summary>
         * ///     启用事务方式类型,0未起事务,1未建新连接起事务,2建新连接且起事务
         * /// </summary>
         * //private int _beginType;
         *
         * //private readonly MySqlDataBaseScope dbScope;*/
        /// <summary>
        ///     构造
        /// </summary>
        /// <param name="dataBase">数据库对象</param>
        private TransactionScope(MySqlDataBase dataBase)
        {
            //dbScope = MySqlDataBaseScope.CreateScope(dataBase);
            //_preScope = CurrentScope;
            //CurrentScope = this;

            //_dataBase = dataBase;
            //dataBase.Open();
            //if (dataBase.Transaction != null)
            //{
            //    return;
            //}
            //_beginType = 1;
            //dataBase.Transaction = dataBase.Connection.BeginTransaction();
        }
        /// <summary>
        ///     AddParameter
        /// </summary>
        /// <param name="name">参数名(无名称使用序号)</param>
        /// <param name="value">参数值</param>
        /// <returns>True表示设置到旧参数,False表示增加了一个参数</returns>
        public bool SetParameterValue(string name, object value)
        {
            if (name == null)
            {
                name = NewParameterName;
                _parameters.Add(name, MySqlDataBase.CreateParameter(name, value));
                return(false);
            }
            MySqlParameter parameter;

            if (_parameters.TryGetValue(name, out parameter))
            {
                parameter.Value = value;
                return(true);
            }
            _parameters.Add(name, MySqlDataBase.CreateParameter(name, value));
            return(false);
        }
Esempio n. 9
0
        /// <summary>
        ///     更新数据
        /// </summary>
        /// <param name="entity">更新数据的实体</param>
        protected bool InsertInner(TData entity)
        {
            PrepareSave(entity, DataOperatorType.Insert);
            using (var cmd = DataBase.CreateCommand())
            {
                var isIdentitySql = SetInsertCommand(entity, cmd);
                MySqlDataBase.TraceSql(cmd);
                if (isIdentitySql)
                {
                    var key = cmd.ExecuteScalar();
                    if (key == DBNull.Value || key == null)
                    {
                        return(false);
                    }
                    entity.SetValue(KeyField, key);
                }
                else
                {
                    if (cmd.ExecuteNonQuery() == 0)
                    {
                        return(false);
                    }
                }

                var sql = AfterUpdateSql(PrimaryKeyConditionSQL);
                if (!string.IsNullOrEmpty(sql))
                {
                    using (var cmd2 = DataBase.CreateCommand())
                    {
                        cmd2.CommandText = sql;
                        cmd2.Parameters.Add(CreatePimaryKeyParameter(entity.GetValue(KeyField)));
                        cmd2.ExecuteNonQuery();
                    }
                }
            }
            EndSaved(entity, DataOperatorType.Insert);
            return(true);
        }
 /// <summary>
 ///     生成一个范围
 /// </summary>
 /// <param name="dataBase">数据库对象</param>
 /// <returns>范围</returns>
 public static MySqlDataBaseScope CreateScope(MySqlDataBase dataBase)
 {
     return(new MySqlDataBaseScope(dataBase));
 }