コード例 #1
0
        public void Add(UsersLesson usersLesson)
        {
            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                var command = connection.CreateCommand();

                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "dbo.InsertUsersLesson";

                SqlParameter parameterIdUser = new SqlParameter
                {
                    DbType        = DbType.Int32,
                    ParameterName = "@IdUser",
                    Value         = usersLesson.Id,
                    Direction     = ParameterDirection.Input
                };
                command.Parameters.Add(parameterIdUser);

                SqlParameter parameterIdLesson = new SqlParameter
                {
                    DbType        = DbType.Int32,
                    ParameterName = "@IdLesson",
                    Value         = usersLesson.IdLesson,
                    Direction     = ParameterDirection.Input
                };
                command.Parameters.Add(parameterIdLesson);

                try
                {
                    connection.Open();

                    command.ExecuteNonQuery();

                    Logs.Log.Info("UsersLesson relationship added");
                }
                catch (SqlException ex)
                {
                    Logs.Log.Error(ex.Message);
                    if (ex.Number == 2627)
                    {
                        throw new UniqueIdentifierException("Such a pair of values is already in the table");
                    }
                    throw;
                }
                catch (Exception ex)
                {
                    Logs.Log.Error(ex.Message);
                    throw;
                }
            }
        }
コード例 #2
0
 public void Add(UsersLesson lesson, out ICollection <Error> listError)
 {
     listError = new List <Error>();
     try
     {
         _usersLessonDao.Add(lesson);
     }
     catch (UniqueIdentifierException)
     {
         listError.Add(new Error
         {
             Message = "You are already signed up for this lesson"
         });
     }
     catch
     {
         listError.Add(new Error
         {
             Message = "Internal error, try again"
         });
     }
 }