Пример #1
0
        protected IList <T> Load(ProcedureParameters parameters)
        {
            List <T> list;

            DbDataReader rdr = null;

            try
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                {
                    DbCommand dbCommand = InitializeDbCommand(sqlConnection, _loadProcedure, parameters);

                    sqlConnection.Open();

                    rdr  = dbCommand.ExecuteReader();
                    list = _mapper.CreatedDataStorageObjects(rdr);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Load list error", ex);
                throw;
            }
            finally
            {
                if (rdr != null && !rdr.IsClosed)
                {
                    rdr.Close();
                    rdr.Dispose();
                }
            }

            return(list);
        }
Пример #2
0
        protected bool Delete(ProcedureParameters parameters)
        {
            try
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                {
                    DbCommand dbCommand = InitializeDbCommand(sqlConnection, _deleteProcedure, parameters);

                    sqlConnection.Open();

                    dbCommand.ExecuteNonQuery();
                }

                return(true);
            }
            catch (Exception ex)
            {
                _logger.Error("Delete error", ex);
                throw;
            }
        }
Пример #3
0
        private static SqlCommand InitializeDbCommand(
            SqlConnection sqlConnection,
            string storedProcedure,
            ProcedureParameters parameters)
        {
            var sqlCommand = sqlConnection.CreateCommand();

            sqlCommand.CommandType    = CommandType.StoredProcedure;
            sqlCommand.CommandText    = storedProcedure;
            sqlCommand.CommandTimeout = 30;

            foreach (var parameter in parameters)
            {
                var dbParameter = sqlCommand.CreateParameter();
                dbParameter.ParameterName = parameter.Key;
                dbParameter.Value         = parameter.Value;
                sqlCommand.Parameters.Add(dbParameter);
            }

            return(sqlCommand);
        }