在c#中创建事务,用于执行一系列函数调用(全部或没有)

本文关键字:全部 函数调用 一系列 执行 创建 事务 用于 | 更新日期: 2023-09-27 18:06:34

我想在我的代码中实现一个由函数分隔的事务。有一个函数调用所有其他函数。我想让这个函数在事务中执行函数调用(全部或无)。一些函数调用是查询,而一些是c#和Crystal报告的例程代码。有人能帮帮忙吗?

这个函数工作得很好。但是,如果两个用户同时访问同一个数据库并执行此函数,可能会出现Cuncurrency问题。

附函数代码。

public bool Generate_PDF(decimal wo_id){

        if (Fill_WO_Report_Table(wo_id) == false)   // this function has queries
            return false;
        try
        {
            string exception;
            cryRpt = new ReportDocument();
            cryRpt.Load(Application.StartupPath + "''CrRpt_WO_Report.rpt");
            DB_Interface.CrystalReport_Login(ref cryRpt, out exception);
            string Temp_file_path = @"c:'Reports'WO_Temp.pdf";
            cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Temp_file_path);
            string File_name = str_WO_File_Name + ".pdf";
            // option to print at which location
            //DialogResult dlg = MessageBox.Show("Select Option For Print :'n'nYes - Print To Default Path'nNo - Print To Custom Path'nCancel - Do Not Print",
            //                                        "Print GRN", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            SaveToDialog frm = new SaveToDialog(ref File_name);
            DialogResult dlg = frm.ShowDialog();
            File_name = frm.GetString();
            frm.Dispose();
            frm = null;
            if (dlg == DialogResult.Yes)
            {
                if (Convert.ToString(MMS_Vars.PathReport_WO) == "")
                    throw new Exception("Default Path Not Available.");
                string Full_File_name = Convert.ToString(MMS_Vars.PathReport_WO) + "''" + File_name;
                cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Full_File_name);
                System.Diagnostics.Process.Start(Full_File_name);
            }
            else if (dlg == DialogResult.No)
            {
                SaveFileDialog obj_saveFileDialog = new SaveFileDialog();
                obj_saveFileDialog.InitialDirectory = Convert.ToString(MMS_Vars.PathReport_WO);
                //obj_saveFileDialog.RestoreDirectory = true;
                obj_saveFileDialog.FileName = File_name;    //set file name to 
                obj_saveFileDialog.Filter = "PDF Files|*.pdf";
                DialogResult result = obj_saveFileDialog.ShowDialog();
                if (result == DialogResult.OK && obj_saveFileDialog.FileName != "")
                {
                    cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, obj_saveFileDialog.FileName);
                    System.Diagnostics.Process.Start(obj_saveFileDialog.FileName);
                }
                else if (result == DialogResult.Cancel)
                {
                    obj_saveFileDialog.Dispose();
                    cryRpt.Close();
                    cryRpt.Dispose();
                    return false;
                }
                obj_saveFileDialog.Dispose();
            }
            else if (dlg == DialogResult.Cancel)
            {
                cryRpt.Close();
                cryRpt.Dispose();
                return false;
            }
            cryRpt.Close();
            cryRpt.Dispose();
            // Drop Report tables
            if (Drop_WO_Report_Table() == false)    // this function has queries
                return false;
            return true;
        }
        catch (Exception exe)
        {
            MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
    }

在c#中创建事务,用于执行一系列函数调用(全部或没有)

您还没有发布您在数据库上执行的任何函数。有写吗?从您发布的代码中,添加事务可能无法解决您的问题。一个简单的解决方案是在函数周围添加一个锁,这样每次只创建一个pdf:

 private static object locker = new object();
 public bool Generate_PDF(decimal wo_id)
 {
      lock(locker){
        if (Fill_WO_Report_Table(wo_id) == false)   // this function has queries
            return false;
            ........
            ........
            return true;
        }
        catch (Exception exe)
        {
            MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
      }//END LOCK
    }