首页 > 解决方案 > 如何同时运行 2 个成功的操作?

问题描述

目标:

我的目标是使用 C# 创建一个 MySQL 查询,在成功查询时我想使用File.Copy...,反之亦然。

这是我当前的代码:

//Try run code
try
{
    //Add record to MySQL
    using (var conn = new MySqlConnection(ConnectionString.ConnString))
    {
        using (var cmd = new MySqlCommand("INSERT INTO files (document_name, path, version, section, user_modified, date_modified)" +
                                        " values (@doc, @path, @version, @section, @user, NOW());", conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@doc", docName.Text);
            cmd.Parameters.AddWithValue("@path", $"{finalPath}Section {Section.Text}\\{docName.Text}{Path.GetExtension(fileName)}");
            cmd.Parameters.AddWithValue("@version", versionNumber.Text);
            cmd.Parameters.AddWithValue("@section", Section.Text);
            cmd.Parameters.AddWithValue("@user", UserDetails.userId);
            cmd.ExecuteNonQuery();
        }

    }

    //Copy file into new directory
    File.Copy(fileName, $"{finalPath}Section {Section.Text}\\{docName.Text}{Path.GetExtension(fileName)}");
}

catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return;
}

问题:

什么是实现以下目标的好方法..

如何做到这一点?

编辑 2020 年 10 月 6 日

我设法得到以下代码:

 //Try run MySQL query with roll back function if failed.
        public void RunTransaction(string myConnString)
        {
            MySqlConnection myConnection = new MySqlConnection(ConnectionString.ConnString);
            myConnection.Open();

            MySqlCommand myCommand = myConnection.CreateCommand();
            MySqlTransaction myTrans;

            // Start a local transaction
            myTrans = myConnection.BeginTransaction();
            // Must assign both transaction object and connection
            // to Command object for a pending local transaction
            myCommand.Connection = myConnection;
            myCommand.Transaction = myTrans;

            try
            {
                myCommand.CommandText = "INSERT INTO files (document_name, path, version, section, user_modified, date_modified, review_date)" +
                                        " values (@doc, @path, @version, @section, @user, NOW(), NOW() + interval 12 month);";
                myCommand.Parameters.AddWithValue("@doc", docName.Text);
                myCommand.Parameters.AddWithValue("@path", $"{finalPath}Section {Section.Text}\\{docName.Text}{Path.GetExtension(fileName)}");
                myCommand.Parameters.AddWithValue("@version", versionNumber.Text);
                myCommand.Parameters.AddWithValue("@section", Section.Text);
                myCommand.Parameters.AddWithValue("@user", UserDetails.userId);
                myCommand.ExecuteNonQuery();
                myTrans.Commit();
            }
            catch (Exception e)
            {
                try
                {
                    myTrans.Rollback();
                }
                catch (MySqlException ex)
                {
                    if (myTrans.Connection != null)
                    {
                        Console.WriteLine("An exception of type " + ex.GetType() +
                        " was encountered while attempting to roll back the transaction.");
                    }
                }

                Console.WriteLine("An exception of type " + e.GetType() +
                " was encountered while inserting the data.");
                Console.WriteLine("Neither record was written to database.");
            }
            finally
            {
                myConnection.Close();
            }
        }

问题..

然后我将如何实现该File.Copy功能以适应我的目标?

我有这个来源.. https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.copyto?view=netcore-3.1

但不确定在我当前的代码中在哪里编码?

标签: c#mysql

解决方案


推荐阅读