Exemplo n.º 1
0
        /// <summary>
        /// Updates work event information in database.
        /// </summary>
        /// <param name="eventDetails">Work event details.</param>
        /// <returns>True if information was updated; false, otherwise.</returns>
        public override bool UpdateEvent(WorkEventDetails eventDetails)
        {
            using (SqlConnection connection = new SqlConnection(this.ConnectionString))
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                SqlCommand command = connection.CreateCommand();
                command.Transaction = transaction;
                command.CommandText =
                    string.Format(
                        "UPDATE {0} SET Name=@Name, BeginTime=@BeginTime, EndTime=@EndTime, Duration=@Duration, UserID=@UserID, ProjectID=@ProjectID, WorkCategoryID=@WorkCategoryID, UptimeEventTypeID=@UptimeEventTypeID WHERE ID=@ID",
                        DBEventsTableName);
                command.Parameters.Add("@ID", SqlDbType.Int).Value = eventDetails.ID;
                command.Parameters.Add("@Name", SqlDbType.NVarChar).Value =
                    eventDetails.Name;
                command.Parameters.Add("@BeginTime", SqlDbType.DateTime).Value =
                    eventDetails.BeginTime;
                command.Parameters.Add("@EndTime", SqlDbType.DateTime).Value =
                    eventDetails.EndTime;
                command.Parameters.Add("@Duration", SqlDbType.Int).Value =
                    eventDetails.Duration;
                command.Parameters.Add("@UserID", SqlDbType.Int).Value =
                    eventDetails.UserID;
                command.Parameters.Add("@ProjectID", SqlDbType.Int).Value =
                    eventDetails.ProjectID;
                command.Parameters.Add("@WorkCategoryID", SqlDbType.Int).Value =
                    eventDetails.WorkCategoryID;
                command.Parameters.Add("@UptimeEventTypeID", SqlDbType.Int).Value =
                    eventDetails.UptimeEventTypeID;

                try
                {
                    bool result = (ExecuteNonQuery(command) == 1);

                    if (result)
                    {
                        transaction.Commit();
                        DeleteEventIfExist(eventDetails.UserID, eventDetails.BeginTime);
                    }
                    else
                        transaction.Rollback();

                    return result;
                }
                catch
                {
                    transaction.Rollback();
                    return false;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates new work event in database.
        /// </summary>
        /// <param name="eventDetails">Work event details.</param>
        /// <returns>ID of new database record.</returns>
        public override int CreateEvent(WorkEventDetails eventDetails)
        {
            int id = -1;
            using (SqlConnection connection = new SqlConnection(this.ConnectionString))
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                SqlCommand command = connection.CreateCommand();
                command.Transaction = transaction;
                command.CommandText =
                    string.Format(
                        "INSERT INTO {0} (Name, BeginTime, EndTime, Duration, UserID, ProjectID, WorkCategoryID, UptimeEventTypeID) VALUES  (@Name, @BeginTime, @EndTime, @Duration, @UserID, @ProjectID, @WorkCategoryID, @UptimeEventTypeID) SELECT @@IDENTITY",
                        DBEventsTableName);
                command.Parameters.Add("@Name", SqlDbType.NVarChar).Value =
                    eventDetails.Name;
                command.Parameters.Add("@BeginTime", SqlDbType.DateTime).Value =
                    eventDetails.BeginTime;
                command.Parameters.Add("@EndTime", SqlDbType.DateTime).Value =
                    eventDetails.EndTime;
                command.Parameters.Add("@Duration", SqlDbType.Int).Value =
                    eventDetails.Duration;
                command.Parameters.Add("@UserID", SqlDbType.Int).Value =
                    eventDetails.UserID;
                command.Parameters.Add("@ProjectID", SqlDbType.Int).Value =
                    eventDetails.ProjectID;
                command.Parameters.Add("@WorkCategoryID", SqlDbType.Int).Value =
                    eventDetails.WorkCategoryID;
                command.Parameters.Add("@UptimeEventTypeID", SqlDbType.Int).Value =
                    eventDetails.UptimeEventTypeID;

                try
                {
                    id = Convert.ToInt32(ExecuteScalar(command));
                    transaction.Commit();
                    DeleteEventIfExist(eventDetails.UserID, eventDetails.BeginTime);
                }
                catch
                { transaction.Rollback(); }
            }
            return id;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates work event details from data reader.
        /// </summary>
        /// <param name="reader">Data reader.</param>
        /// <returns>Work event details.</returns>
        protected virtual WorkEventDetails GetDetailsFromReader(IDataReader reader)
        {
            WorkEventDetails details = new WorkEventDetails();

            details.ID = (int)reader["ID"];
            details.Name = (string)reader["Name"];
            details.BeginTime = (DateTime)reader["BeginTime"];
            details.EndTime = (DateTime)reader["EndTime"];
            details.Duration = (int)reader["Duration"];
            details.UserID = (int)reader["UserID"];
            details.ProjectID = (int)reader["ProjectID"];
            details.WorkCategoryID = (int)reader["WorkCategoryID"];
            details.UptimeEventTypeID = (int)reader["UptimeEventTypeID"];

            return details;
        }