Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以设定的延迟更新日期_C#_Mysql - Fatal编程技术网

C# 以设定的延迟更新日期

C# 以设定的延迟更新日期,c#,mysql,C#,Mysql,在一个使用MySQL的windows窗体.net应用程序中,我的产品具有多个制造操作。每个操作都有一个预期的开始日期和预期的完成日期。我想做的是,当用户更改第一次操作的日期时,更改该产品后续操作的开始日期和结束日期(推送日期)。到目前为止,我已经设法更改了以下操作的日期,但它们都是相同的。如果你能帮我写一些代码,那就太好了。我应该改用Datatable吗?以下是我到目前为止得到的信息: #region Push the predicted start date in planning tabl

在一个使用MySQL的windows窗体.net应用程序中,我的产品具有多个制造操作。每个操作都有一个预期的开始日期和预期的完成日期。我想做的是,当用户更改第一次操作的日期时,更改该产品后续操作的开始日期和结束日期(推送日期)。到目前为止,我已经设法更改了以下操作的日期,但它们都是相同的。如果你能帮我写一些代码,那就太好了。我应该改用Datatable吗?以下是我到目前为止得到的信息:

 #region Push the predicted start date in planning table for same product id if end date is pushed forward
        DateTime oldStartDate = DateTime.Now;
        DateTime oldFinishDate = DateTime.Now;
        DateTime dt3 = DateTime.Parse(predicted_delivery.Text);
        DateTime dt4 = DateTime.Parse(new_predicted_delivery.Text);
        if(dt3.Date == dt4.Date)
        {
            return;
        }
        else
        {
            int delay = (dt4 - dt3).Days;

            ConnectionStringSettings conSettings1 = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
            MySqlConnection con1 = new MySqlConnection(conSettings1.ToString());
            MySqlCommand cmd1 = new MySqlCommand(@"select * from shopmanager.planning where part_id = @part_id;", con1);
            MySqlDataReader myReader1;

            try
            {
                con1.Open();
                cmd1.Parameters.AddWithValue("@part_id", temp_part.item_id);
                myReader1 = cmd1.ExecuteReader();

                while (myReader1.Read())
                {
                    oldStartDate = myReader1.GetDateTime("predicted_start_date");
                    oldFinishDate = myReader1.GetDateTime("predicted_delivery");
                }
                cmd1.Parameters.Clear();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            con1.Close();
            newStartDate = oldStartDate.AddDays(delay);
            newFinishDate = oldFinishDate.AddDays(delay);


            UpdateNewStartDate();
        }
        #endregion

    }
    #endregion

    #region Update new start and finish dates
    private void UpdateNewStartDate()
    {
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
        MySqlConnection con = new MySqlConnection(conSettings.ToString());
        MySqlCommand cmd = new MySqlCommand(@"update shopmanager.planning set predicted_start_date = @predicted_start_date, predicted_delivery = @predicted_delivery where part_id = @part_id", con);

        try
        {
            con.Open();
            cmd.Parameters.AddWithValue("@predicted_start_date", newStartDate);
            cmd.Parameters.AddWithValue("@predicted_delivery", newFinishDate);
            cmd.Parameters.AddWithValue("@part_id", temp_part.item_id);

            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        con.Close();

这段代码中有很多内容,但我认为问题在于您读取了所有行,然后只进行了一次计算和一次更新

如果在C#中进行计算,则需要对每一行进行计算,如下所示:

           while (myReader1.Read())
            {
                oldStartDate = myReader1.GetDateTime("predicted_start_date");
                oldFinishDate = myReader1.GetDateTime("predicted_delivery");

                newStartDate = oldStartDate.AddDays(delay);
                newFinishDate = oldFinishDate.AddDays(delay);

                UpdateNewStartDate(partId, newStartDate, newFinishDate);
            }
            cmd1.Parameters.Clear();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        con1.Close();
除了'UpdateNewStartDate();'更新所有行,因此需要添加一个参数来指定要更新的行

更好的方法可能是在存储过程中执行,因此您只需指定任务和延迟,它就会更新日期


另外,一般来说,它有助于将数据库访问代码从业务逻辑中分离出来,从而使函数更小、更易于阅读。UpdateNewStartDate应该真正从参数中获取所有输入,而不是使用与其他函数共享的变量-当您无法分辨设置的位置时,这会让人感到困惑。

我使用了上述解决方案,它现在可以工作了。我唯一更改的是partId到op-id。所有内容都在完美更新。谢谢