我想在我的代码中实现一个由函数分隔的事务。 有一个函数可以调用所有其他函数。我想让这个函数在事务中执行函数调用(全部或无)。 一些函数调用是查询,而一些函数调用是C#和Crystal报表的例程代码。 有人可以帮忙吗?
此功能完美运行。 但是如果2个用户正在访问同一个数据库并且同时执行此功能可能会出现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;
}
}
答案 0 :(得分:0)
您尚未发布您在数据库中执行的任何功能。有没有写等等?从您发布的代码中,添加交易可能无法解决您的问题。一个简单的解决方案是在函数周围添加一个锁,这样一次只能创建一个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
}