示例#1
0
        /// <summary>
        /// This method executes a stored procedure that does not return any data
        /// </summary>
        /// <param name="connectionStringStringType">The connection string type</param>
        /// <param name="storedProcedure">Stored procedure's name</param>
        /// <param name="namedParams">Parameter values</param>
        private static int ExecuteNonQuery(string storedProcedure, Dictionary <string, object> namedParams)
        {
            try
            {
                using (var connection = CreateConnection())
                {
                    using (IDbCommand command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = storedProcedure;

                        AddParamethers(command, namedParams);

                        var param = command.CreateParameter();
                        param.DbType        = DbType.Int32;
                        param.Direction     = ParameterDirection.ReturnValue;
                        param.ParameterName = "@Return";
                        command.Parameters.Add(param);

                        command.Prepare();
                        command.ExecuteNonQuery();

                        param = (IDbDataParameter)command.Parameters["@Return"];
                        return((int)param.Value);
                    }
                }
            }
            catch (Exception ex)
            {
                var e = new DalException(storedProcedure, ex);
                Logger.LogException(e);
                throw e;
            }
        }
示例#2
0
        public void ExecuteFailNonQueryConnectionNotOpenedByCtorSuccess()
        {
            using Dal db = new Dal();
            DalException ex = Assert.Throws <DalException>(() => db.ExecuteNonQuery("Select toperr 1 * from offers WITH (NOLOCK)"));

            Assert.That(ex, Is.Not.Null);
        }
示例#3
0
        public void ConnectionCloseFail()
        {
            Dal          db = new Dal(true);
            DalException ex = Assert.Throws <DalException>(() => db.CloseConnection());

            Assert.That(ex, Is.Not.Null);
        }
示例#4
0
 public void ExecuteScalarNoParamsThrowBadQuery()
 {
     using (Dal db = new Dal())
     {
         DalException ex = Assert.Throws <DalException>(() => db.ExecuteScalar("Select bad query"));
         Assert.IsTrue(ex.Message.Contains("Invalid"));
     }
 }
示例#5
0
 public void ExecuteScalarNoParamsThrowSecurityError()
 {
     using (Dal db = new Dal())
     {
         DalException ex = Assert.Throws <DalException>(() => db.ExecuteScalar("DROP DATABASE TEST"));
         Assert.IsTrue(ex.Message.Contains("Security"));
     }
 }
示例#6
0
        public void ConnectionToNotExistsDbCorrectConnectionString()
        {
            using Dal db =
                      new Dal(
                          "Data Source=13.12.122.228;Initial Catalog=Platinum;User id=scheduler;Password=64!;Connection Timeout=5");
            DalException ex = Assert.Throws <DalException>(() => db.OpenConnection());

            Assert.That(ex, Is.Not.Null.Or.Empty);
        }
示例#7
0
        public void ExecuteReaderTestDropQueryFail()
        {
            using Dal db = new Dal();
            db.OpenConnection();
            DalException ex = Assert.Throws <DalException>(() => db.ExecuteReader("Drop database masterr"));

            Assert.That(ex, Is.Not.Null);
            Assert.That(ex.Message, Contains.Substring("cannot drop databases"));
        }
示例#8
0
        public void ExecuteReaderExceptionBadQuery()
        {
            using Dal db = new Dal(true);
            db.OpenConnection();

            DalException ex = Assert.Throws <DalException>(() => db.ExecuteReader("SELECTMISSPELL TOP 1 * FROM websiteCategories WHERE Id = -9"));

            Assert.That(ex, Is.Not.Null);
        }
        public void CheckNotFoundCategoryException()
        {
            OfferCategory offerCategory;
            DalException  ex = Assert.Throws <DalException>(() =>
                                                            offerCategory = new OfferCategory(EOfferWebsite.Allegro, -1)
                                                            );

            Assert.That(ex, Is.Not.Null);
        }
示例#10
0
 /// <summary>
 /// DB错误
 /// </summary>
 /// <param name="dalException">消息</param>
 /// <returns></returns>
 public static ApiResponse DbFailure(DalException dalException = null)
 {
     return(new ApiResponse
     {
         Code = (int)ResponseCode.DBFailure,
         Status = "fail",
         Data = null,
         Message = dalException?.ToString()
     });
 }
示例#11
0
        public void DoubleCloseConnection()
        {
            Dal db = new Dal(true);

            db.OpenConnection();
            db.CloseConnection();
            DalException ex = Assert.Throws <DalException>(() => db.CloseConnection());

            Assert.That(ex, Is.Not.Null);
            db.Dispose();
        }
        public void GetSpecific_ForArticle_NotRegistered()
        {
            RepositoriesSet repositoriesSet = new RepositoriesSet();

            DalException ex = Assert.Throws <DalException>(() =>
            {
                repositoriesSet.GetGeneric <Article>();
            });

            Assert.That(ex.errorType, Is.EqualTo(DalErrorType.RepositoriesSetMissingMapping));
            Assert.That(ex.Message, Is.EqualTo("Instance is missing for GenericStructure.Models.CoreBusiness.Article"));
        }
 public void Create(DalException e)
 {
     var exception = new Exception()
     {
         ExceptionMessage = e.ExceptionMessage,
         ControllerName = e.ControllerName,
         ActionName = e.ActionName,
         StackTrace = e.StackTrace,
         Date = e.Date
     };
     _context.Set<Exception>().Add(exception);
 }
示例#14
0
        public void DoubleBeginTransaction()
        {
            Dal db = new Dal(true);

            db.OpenConnection();
            db.BeginTransaction();

            DalException ex = Assert.Throws <DalException>(() => db.BeginTransaction());

            Assert.That(ex.Message, Is.Not.Null.Or.Empty);
            db.Dispose();
        }
示例#15
0
        public void GetSpecific_ForCategory_NotRegistered()
        {
            RepositoriesSet repositoriesSet = new RepositoriesSet();

            DalException ex = Assert.Throws <DalException>(() =>
            {
                repositoriesSet.GetGeneric <DbCategory>();
            });

            Assert.That(ex.errorType, Is.EqualTo(DalErrorType.RepositoriesSetMissingMapping));
            Assert.That(ex.Message, Is.EqualTo("Instance is missing for ImgBoard.Dal.Models.Main.DbCategory"));
        }
示例#16
0
        public void Db_ErrorsReportingService_CreateApplicationAsync_AlreadyExists()
        {
            using (ThreadScopedLifestyle.BeginScope(container))
            {
                ErrorsReportingService service = container.GetInstance <ErrorsReportingService>();

                DalException ex = Assert.ThrowsAsync <DalException>(async() =>
                {
                    await service.CreateApplicationAsync("TestApplicationAlreadyExisting", "a.a.a.a");
                });
                Assert.That(ex.errorType, Is.EqualTo(DalErrorType.SqlUniqueConstraintViolation));
            }
        }
示例#17
0
        public void UpdateOfferDatabaseWithInvalidString()
        {
            controller.StartFetching(true,
                                     new OfferCategory(EOfferWebsite.Allegro,
                                                       mainCategory));
            List <string> testList = new List <string>()
            {
                "{',''][[[]{{}",
            };
            DalException ex = Assert.Throws <DalException>(() => controller.UpdateDatabaseWithOffers(testList));

            Assert.That(ex, Is.Not.Null);
        }
示例#18
0
        public void Db_ErrorsReportingService_CreateApplicationAsync_AlreadyExists()
        {
            using (IUnityContainer childContainer = this.container.CreateChildContainer())
            {
                IErrorsReportingService service = childContainer.Resolve <IErrorsReportingService>();

                DalException ex = Assert.ThrowsAsync <DalException>(async() =>
                {
                    await service.CreateApplicationAsync("TestApplicationAlreadyExisting", "a.a.a.a");
                });
                Assert.That(ex.errorType, Is.EqualTo(DalErrorType.SqlUniqueConstraintViolation));
            }
        }
示例#19
0
 public void Error(DalException exception)
 {
     IsOk     = false;
     Response = default(TResponse);
     Message  = exception.Mensaje;
     if (exception.ErrorOrigen == null)
     {
         Logger.Error(Message);
     }
     else
     {
         Logger.Error(Message, exception.ErrorOrigen);
     }
 }
示例#20
0
        public void CreateApplicationAsync_AlreadyExisting()
        {
            VolatileErrorsReportingDataset store = new VolatileErrorsReportingDataset();

            Mock <IErrorsReportingService> mockService = new Mock <IErrorsReportingService>();

            mockService.Setup(s => s.CreateApplicationAsync(It.IsIn <string>(store.Applications.Select(a => a.Name)), It.IsAny <string>()))
            .Throws(new DalException(DalErrorType.SqlUniqueConstraintViolation, "Application already exists"));

            IErrorsReportingService service = mockService.Object;

            DalException ex = Assert.ThrowsAsync <DalException>(async() =>
            {
                await service.CreateApplicationAsync("TestApplicationAlreadyExisting", "1.0.0.0");
            });

            Assert.That(ex.errorType, Is.EqualTo(DalErrorType.SqlUniqueConstraintViolation));
        }
示例#21
0
        public async Task Db_SalesService_Concurrency_NoPolicy()
        {
            using (IUnityContainer childContainer = this.container.CreateChildContainer())
            {
                IBaseMainService service = childContainer.Resolve <IBaseMainService>();
                service.SetPolicy(DataConflictPolicy.NoPolicy);

                var category = await service.GetByIdAsync <DbCategory>(this.dataSet.CategoriesIds.First());

                category.Title = "User 1 Category Title";

                this.categoriesSqlHelper.ModifyTitle(category.Id, "User 2 Category Title");

                DalException ex = Assert.ThrowsAsync <DalException>(async() =>
                {
                    await service.ModifyAsync(category);
                });
                Assert.That(ex.errorType, Is.EqualTo(DalErrorType.BaseServiceDataConflictWithNoPolicy));
            }
        }
示例#22
0
        public async Task Db_SalesService_Concurrency_NoPolicy()
        {
            using (ThreadScopedLifestyle.BeginScope(container))
            {
                SalesService service = container.GetInstance <SalesService>();
                service.SetPolicy(DataConflictPolicy.NoPolicy);

                var article = await service.GetByIdAsync <Article>(this.dataSet.ArticlesIds.First());

                article.Title = "User1 Title 1";

                this.articlesSqlHelper.ModifyTitle(article.Id, "User2 Title 1");

                DalException ex = Assert.ThrowsAsync <DalException>(async() =>
                {
                    await service.ModifyAsync(article);
                });
                Assert.That(ex.errorType, Is.EqualTo(DalErrorType.BaseServiceDataConflictWithNoPolicy));
            }
        }
示例#23
0
 protected static object ExecuteScalar(string query)
 {
     try
     {
         using (var connection = CreateConnection())
         {
             using (IDbCommand command = connection.CreateCommand())
             {
                 command.CommandText = query;
                 return(command.ExecuteScalar());
             }
         }
     }
     catch (Exception ex)
     {
         var e = new DalException(query, ex);
         Logger.LogException(e);
         throw e;
     }
 }
示例#24
0
        /// <summary>
        /// Execute a stored procedure and returns a DataReader instance
        /// </summary>
        /// <param name="storedProcedure">Stored procedure's name</param>
        /// <param name="namedParams">The parameters dictionary</param>
        /// <param name="connectionStringStringType">The connection string type</param>
        /// <returns></returns>
        private static IDataReader ExecuteReader(string storedProcedure, Dictionary <string, object> namedParams,
                                                 IDbConnection connection, IDbCommand command)
        {
            try
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = storedProcedure;

                AddParamethers(command, namedParams);

                command.Prepare();
                return(command.ExecuteReader());
            }
            catch (Exception ex)
            {
                var e = new DalException(storedProcedure, ex);
                Logger.LogException(e);
                throw e;
            }
        }
示例#25
0
 protected static void ExecuteQuery(string query)
 {
     try
     {
         using (var connection = CreateConnection())
         {
             using (IDbCommand command = connection.CreateCommand())
             {
                 command.CommandText = query;
                 command.ExecuteNonQuery();
             }
         }
     }
     catch (Exception ex)
     {
         var e = new DalException(query, ex);
         Logger.LogException(e);
         throw e;
     }
 }
 public void Update(DalException e)
 {
     throw new NotImplementedException();
 }
 public void Delete(DalException e)
 {
     var exception = _context.Set<Exception>().FirstOrDefault(exc => exc.Id == e.Id);
     _context.Set<Exception>().Remove(exception);
 }
示例#28
0
 public static ExceptionDS.ExceptionDSDataTable GetAllExceptions()
 {
     return(DalException.GetAllExceptions());
 }
示例#29
0
 public static Int32 DeleteException(Int32 exceptionId)
 {
     return(DalException.DeleteException(exceptionId));
 }
示例#30
0
        public void ConnectionToNoExistDb()
        {
            DalException ex = Assert.Throws <DalException>(() => new Dal("test").OpenConnection());

            Assert.That(ex, Is.Not.Null);
        }