Example #1
0
        protected Dictionary <object, T> queryForDictionary(string sql, string keyName)
        {
            MySQLConnection        conn          = null;
            Dictionary <object, T> objDictionary = null;

            try
            {
                conn = this.dbMgr.DBConns.PopDBConnection();
                MySQLCommand    cmd       = new MySQLCommand(sql, conn);
                MySQLDataReader reader    = cmd.ExecuteReaderEx();
                int             columnNum = reader.FieldCount;
                objDictionary = new Dictionary <object, T>();
                string[] nameArray = new string[columnNum];
                while (reader.Read())
                {
                    int    index = 0;
                    T      obj   = Activator.CreateInstance <T>();
                    object key   = null;
                    for (int i = 0; i < columnNum; i++)
                    {
                        int _index = index++;
                        if (null == nameArray[_index])
                        {
                            nameArray[_index] = reader.GetName(_index);
                        }
                        string columnName  = nameArray[_index];
                        object columnValue = reader.GetValue(_index);
                        this.setValue(obj, columnName, columnValue);
                        if (null != key)
                        {
                            if (keyName.Equals(reader.GetName(_index)) || keyName == reader.GetName(_index))
                            {
                                key = reader.GetValue(_index);
                            }
                        }
                    }
                    if (null != key)
                    {
                        objDictionary.Add(key, obj);
                    }
                }
                GameDBManager.SystemServerSQLEvents.AddEvent(string.Format("+SQL: {0}", sql), EventLevels.Important);
                cmd.Dispose();
            }
            catch (Exception)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("查询数据库失败: {0}", sql), null, true);
                return(null);
            }
            finally
            {
                if (null != conn)
                {
                    this.dbMgr.DBConns.PushDBConnection(conn);
                }
            }
            return(objDictionary);
        }
Example #2
0
        /// <summary>
        /// 查询返回多个实例
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>List<T>形式查询结果集s</returns>
        protected List <T> queryForList(string sql)
        {
            MySQLConnection conn = null;
            List <T>        list = null;

            try
            {
                conn = dbMgr.DBConns.PopDBConnection();

                MySQLCommand    cmd    = new MySQLCommand(sql, conn);
                MySQLDataReader reader = cmd.ExecuteReaderEx();

                int columnNum = reader.FieldCount;

                //list = new List<T>();

                while (reader.Read())
                {
                    //索引下标
                    int index = 0;

                    T obj = Activator.CreateInstance <T>();
                    for (int i = 0; i < columnNum; i++)
                    {
                        int    _index      = index++;
                        string columnName  = reader.GetName(_index);
                        Object columnValue = reader.GetValue(_index);

                        if (null == list)
                        {
                            list = new List <T>();
                        }

                        setValue(obj, columnName, columnValue);
                    }

                    list.Add(obj);
                }

                GameDBManager.SystemServerSQLEvents.AddEvent(string.Format("+SQL: {0}", sql), EventLevels.Important);

                cmd.Dispose();
                cmd = null;
            }
            catch (Exception e)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("查询数据库失败: {0},exception:{1}", sql, e));
                return(null);
            }
            finally
            {
                if (null != conn)
                {
                    dbMgr.DBConns.PushDBConnection(conn);
                }
            }

            return(list);
        }
Example #3
0
        protected T queryForObject(string sql)
        {
            MySQLConnection conn = null;
            T obj = default(T);

            try
            {
                conn = this.dbMgr.DBConns.PopDBConnection();
                MySQLCommand    cmd       = new MySQLCommand(sql, conn);
                MySQLDataReader reader    = cmd.ExecuteReaderEx();
                int             columnNum = reader.FieldCount;
                if (reader.Read())
                {
                    int index = 0;
                    for (int i = 0; i < columnNum; i++)
                    {
                        int    _index      = index++;
                        string columnName  = reader.GetName(_index);
                        object columnValue = reader.GetValue(_index);
                        if (null == obj)
                        {
                            obj = Activator.CreateInstance <T>();
                        }
                        this.setValue(obj, columnName, columnValue);
                    }
                }
                GameDBManager.SystemServerSQLEvents.AddEvent(string.Format("+SQL: {0}", sql), EventLevels.Important);
                cmd.Dispose();
            }
            catch (Exception)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("查询数据库失败: {0}", sql), null, true);
                return(default(T));
            }
            finally
            {
                if (null != conn)
                {
                    this.dbMgr.DBConns.PushDBConnection(conn);
                }
            }
            return(obj);
        }