Beispiel #1
0
        /// <summary>
        /// 쿼리를 실행하고 쿼리에서 반환된 결과 집합에서 첫 번째 행의 첫 번째 열을 반환합니다.
        /// </summary>
        /// <typeparam name="T">Generic object, 결과값 형타입</typeparam>
        /// <param name="commandType">CommandType</param>
        /// <param name="commandText">Transact-SQL statement, table name or stored procedure</param>
        /// <param name="parameters">SP(저장프로시저)나 SQL 문 파라메터에 할당되는 object 배열</param>
        /// <returns>T(Generic object) 타입 값</returns>
        public T ExecuteScalar <T>(CommandType commandType, string commandText, params object[] parameters) where T : IConvertible
        {
            try
            {
                _command.CommandType = commandType;
                _command.CommandText = commandText;
                if (commandType == CommandType.Text && parameters != null)
                {
                    ProcessParameters(_provider, _command, commandText, parameters);
                }

                _connection.Open();
                var result = _command.ExecuteScalar();
                return(GlobalConvert.ChangeType <T>(result));
            }
            catch (DbException dbEx)
            {
                if (_handleErrors)
                {
                    _lstError = dbEx.Message;
                }
                else
                {
                    throw dbEx;  // CustomException DB, dbEx
                }
            }
            catch (Exception ex)
            {
                if (_handleErrors)
                {
                    _lstError = ex.Message;
                }
                else
                {
                    throw ex;  // CustomException System, ex
                }
            }
            return(GlobalConvert.DefaultValue <T>());
        }
Beispiel #2
0
 protected T GetValue <T>(object key, T defaultValue) where T : IConvertible
 {
     return(GlobalConvert.ChangeType <T>(this[key], defaultValue));
 }