Example #1
0
        /// <summary>
        /// Вызывает диалоговое окно и производит добавление нового события к списку существующих задач.
        /// </summary>
        private void AddTask()
        { // вызываем диалоговое окно для редактирования параметров новой задачи.
            AddTaskDialog taskDialog = new AddTaskDialog();

            taskDialog.CurrentDate = new DateTime(CurrentYear, CurrentMonth, CurrentDay, 0, 0, 0);

            taskDialog.ShowDialog();
            if (taskDialog.DialogResult != DialogResult.OK)
            {
                return;
            }

            string   strTaskName     = taskDialog.TaskName;
            string   strTaskType     = taskDialog.TaskType;
            string   strTaskLocation = taskDialog.TaskLocation;
            DateTime StartDateTime   = taskDialog.StartDateTime;
            DateTime FinishDateTime  = taskDialog.FinishDateTime;

            taskDialog.Close();

            // Производим сохранение задачи в базе данных

            string        connStr = (string.Format(@"Data Source=(local)\SQLEXPRESS; Initial Catalog= {0}; Integrated Security=True", strDataBaseName));
            SqlConnection conn    = new SqlConnection(connStr);

            try
            {
                statusLabel.Text = "Подключение к БД Diary ";
                conn.Open();
            }
            catch (SqlException se)
            {
                if (se.Number == 4060)
                {
                    statusLabel.Text = "Ошибка подключения к БД";
                    conn.Close();
                    return;
                }
            }

            // узнаем сколько на текущий день задачь уже существует.

            using (SqlCommand cmd = new SqlCommand("INSERT INTO Diary (TaskType,TaskName,StartDateTime,FinishDateTime,Location) Values (@TaskType,@TaskName,@StartDateTime,@FinishDateTime,@Location)", conn))
            {
                /*Работаем с параметрами(SqlParameter), эта техника позволяет уменьшить
                 * кол-во ошибок и достичь большего быстродействия
                 * но требует и больших усилий в написании кода*/

                // объявляем объект класса SqlParameter

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@TaskType"; param.Value = strTaskType; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@TaskName"; param.Value = strTaskName; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@StartDateTime"; param.Value = StartDateTime; param.SqlDbType = SqlDbType.DateTime;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@FinishDateTime"; param.Value = FinishDateTime; param.SqlDbType = SqlDbType.DateTime;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@Location"; param.Value = strTaskLocation; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                statusLabel.Text = "Вставляем запись";

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    statusLabel.Text = "Ошибка, при выполнении запроса на добавление записи";
                    conn.Close();
                    return;
                }
            }

            conn.Close();
            conn.Dispose();

            CalendarTaskReinitialize(StartDateTime, FinishDateTime, true);
            TaskRead();
        }
Example #2
0
        /// <summary>
        /// Редактирование вырбранной задачи.
        /// </summary>
        private void TaskModify()
        {
            int taskID = 0;

            CurrentDay = Convert.ToInt32(dGvCalendar.CurrentCell.Value);

            string connStr = (string.Format(@"Data Source=(local)\SQLEXPRESS; Initial Catalog= {0}; Integrated Security=True", strDataBaseName));

            SqlConnection conn = new SqlConnection(connStr);

            try
            {
                statusLabel.Text = "Подключение к БД Diary";
                conn.Open();
            }
            catch (SqlException se)
            {
                //  if (se.Number == 4060)
                //  {
                statusLabel.Text = "Ошибка подключения к БД";
                conn.Close();
                return;
                //  }
            }

            AddTaskDialog taskDialog = new AddTaskDialog();

            taskID = Convert.ToInt32(taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.ID]);
            taskDialog.TaskName       = (taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.TaskName]).ToString();
            taskDialog.TaskType       = (taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.TaskType]).ToString();
            taskDialog.TaskLocation   = (taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.Location]).ToString();
            taskDialog.StartDateTime  = Convert.ToDateTime(taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.StartDateTime]);
            taskDialog.FinishDateTime = Convert.ToDateTime(taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.FinishDateTime]);



            conn.Close();
            taskDialog.ShowDialog();

            if (taskDialog.DialogResult != DialogResult.OK)
            {
                return;
            }

            string   strTaskName     = taskDialog.TaskName;
            string   strTaskType     = taskDialog.TaskType;
            string   strTaskLocation = taskDialog.TaskLocation;
            DateTime StartDateTime   = taskDialog.StartDateTime;
            DateTime FinishDateTime  = taskDialog.FinishDateTime;


            taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.TaskName]       = strTaskName;
            taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.TaskType]       = strTaskType;
            taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.Location]       = strTaskLocation;
            taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.StartDateTime]  = StartDateTime;
            taskValues.Rows[dGvTasks.CurrentRow.Index].ItemArray[(int)CellsDataBase.FinishDateTime] = FinishDateTime;


            taskDialog.Close();

            try
            {
                statusLabel.Text = "Подключение к БД Diary ";
                conn.Open();
            }
            catch (SqlException se)
            {
                if (se.Number == 4060)
                {
                    statusLabel.Text = "Ошибка подключения к БД";
                    conn.Close();
                    return;
                }
            }

            using (SqlCommand cmd = new SqlCommand("UPDATE Diary Set TaskType = @TaskType , TaskName = @TaskName , StartDateTime = @StartDateTime , FinishDateTime = @FinishDateTime , Location =  @Location WHERE ID = @ID", conn))
            {
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@ID"; param.Value = (taskID); param.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@TaskType"; param.Value = strTaskType; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@TaskName"; param.Value = strTaskName; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@StartDateTime"; param.Value = StartDateTime; param.SqlDbType = SqlDbType.DateTime;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@FinishDateTime"; param.Value = FinishDateTime; param.SqlDbType = SqlDbType.DateTime;
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@Location"; param.Value = strTaskLocation; param.SqlDbType = SqlDbType.Text;
                cmd.Parameters.Add(param);

                statusLabel.Text = "Измененение записи";

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    statusLabel.Text = "Ошибка, при выполнении запроса на изменение записи" /*+ ex.Message.ToString()*/;
                    conn.Close();
                    return;
                }
            }
            conn.Close();
            conn.Dispose();

            if (strTaskLocation != "")
            {
                dGvTasks[0, dGvTasks.Rows.Count - 1].Value = strTaskType + "\r\n" + strTaskLocation;
            }
            else
            {
                dGvTasks[0, dGvTasks.CurrentRow.Index].Value = strTaskType;
            }

            if ((StartDateTime.Day == CurrentDay) && (FinishDateTime.Day == CurrentDay))
            {
                if ((StartDateTime.Hour < FinishDateTime.Hour) && (strTaskType != "Памятка"))
                {
                    dGvTasks[1, dGvTasks.Rows.Count - 1].Value = StartDateTime.Hour.ToString("00") + ":" + StartDateTime.Minute.ToString("00") + "\r\n" + FinishDateTime.Hour.ToString("00") + ":" + FinishDateTime.Minute.ToString("00");
                }
                else
                {
                    dGvTasks[1, dGvTasks.Rows.Count - 1].Value = StartDateTime.Hour.ToString("00") + ":" + StartDateTime.Minute.ToString("00");
                }
            }
            else
            if ((StartDateTime.Day == CurrentDay) && (FinishDateTime.Day > CurrentDay) && (StartDateTime.Month == CurrentMonth))
            {
                dGvTasks[1, dGvTasks.Rows.Count - 1].Value = "Начало \r\n " + StartDateTime.Hour.ToString("00") + ":" + StartDateTime.Minute.ToString("00");
            }
            else
            if ((StartDateTime.Day > CurrentDay) && (FinishDateTime.Day == CurrentDay) && (FinishDateTime.Month == CurrentMonth))
            {
                dGvTasks[1, dGvTasks.Rows.Count - 1].Value = "Окончание \r\n " + FinishDateTime.Hour.ToString("00") + ":" + FinishDateTime.Minute.ToString("00");
            }
            else // if ((StartTime.Day < CurrentDay) && (FinishTime.Day > CurrentDay))
            {
                dGvTasks[1, dGvTasks.Rows.Count - 1].Value = "Весь день";
            }

            dGvTasks[2, dGvTasks.CurrentRow.Index].Value = strTaskName;

            dGvTasks.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);

            CalendarTaskReinitialize(StartDateTime, FinishDateTime, true);
        }