c# - 如何同时运行 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;
}
问题:
什么是实现以下目标的好方法..
- 如果 MySQL 查询不成功,则不执行任何操作并停止并返回错误所在的位置。
- 如果 MySQL 查询成功并且
File.Copy
没有成功(由于任何原因遇到错误) - 然后恢复查询,删除插入的任何内容,然后一直返回到第一步。
如何做到这一点?
编辑 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
但不确定在我当前的代码中在哪里编码?
解决方案
推荐阅读
- ios - Firebase 未在 iOS 应用中触发推送通知
- typescript - BlinkID 插件错误 Ionic 4 - Native:尝试调用 BlinkId.BarcodeRecognizer,但未安装 BlinkId 插件
- javascript - RxJS:创建和订阅可观察的 onClick
- java - 查找具有最小最大点的两个边界框之间的距离
- string - valueerror:无法在 python 2.7 中将字符串转换为浮点数
- python - 工作 Azure HTTP 函数应用在发布时中断
- database - Sembast 中的 put 和 add 有什么区别?
- reactjs - 为什么我不能在状态 Reactjs 中保存这个请求
- javascript - npm 在安装包时显示错误
- python - 如何使用烧瓶获取上传文件的绝对url路径