提问人:Md Thousif 提问时间:11/13/2023 最后编辑:Md Thousif 更新时间:11/16/2023 访问量:61
如何在 C# 中使用线程同时执行存储过程 [已关闭]
How to execute stored procedure simultaneously using threads in C# [closed]
问:
使用线程在 3 个不同的表中同时插入数据
public class ProcessThreads
{
Dictionary<string, string> dic = new Dictionary<string, string>();
//DataLibrary[] _dbs = new DataLibrary[10];
#region Variable Decleration
IRIZID.DataLib.DataLibrary _db = new DataLibrary();
#endregion
public ProcessThreads()
{
string cf = @"C:\Program Files (x86)\IRIZ ID\TNAS\Connections";
string[] dsFiles = Directory.GetFiles(cf, "*.ds");
List<Thread> threads = new List<Thread>();
foreach (string dsFile in dsFiles)
{
string fileName = Path.GetFileNameWithoutExtension(dsFile);
string fileContent = File.ReadAllText(dsFile);
dic.Add(fileName, fileContent);
}
foreach (string value in dic.Keys)
{
Thread thread = new Thread(() => connection(value));
thread.Name = value.Substring(0, 4);
threads.Add(thread);
}
foreach (Thread thread in threads)
{
thread.Start();
}
}
void connection(string fileName)
{
ProcessPunch processPunch = new ProcessPunch(fileName);
ProcessPunchFILO processPunchFilo = new ProcessPunchFILO(fileName);
}
}
public class ProcessPunch
{
#region Global Variables
DataTable dataTable;
DataLibrary dataLibrary;
DateTime PDate = new DateTime();
#endregion
public ProcessPunch(string fileName)
{
try
{
bool connStatus = false;
dataLibrary = new DataLibrary(fileName, false, out connStatus);
//dataLibrary = new DataLibrary(true, fileName);
string queryString = string.Empty;
queryString = "EXEC usp_TNA_ProcessPunch";
dataLibrary.ExecuteNonQuery(queryString);
}
catch (ApplicationException ae)
{
WriteLogs.WriteErrorLogs(-1, 0, GetType().FullName + "." + MethodBase.GetCurrentMethod().Name, ae.ToString());
}
catch (Exception e)
{
WriteLogs.WriteErrorLogs(-1, 0, GetType().FullName + "." + MethodBase.GetCurrentMethod().Name, e.ToString());
}
finally
{
dataLibrary.FinalizeDataService();
}
}
}
public class ProcessPunchFILO
{
#region Global Variables
DataTable dataTable;
DataTable dataTablePdate;
DataTable dataTableTransaction;
DataLibrary dataLibrary;
DateTime PDate = new DateTime();
#endregion
public ProcessPunchFILO(string fileName)
{
// ProcessPunchFILOTransaction();
try
{
bool connStatus = false;
dataLibrary = new DataLibrary(fileName, false, out connStatus);
//dataLibrary = new DataLibrary(true, fileName);
string queryString = string.Empty;
queryString = "EXEC usp_TNA_ProcessPunchFILO";
dataLibrary.ExecuteNonQuery(queryString);
}
catch (ApplicationException ae)
{
WriteLogs.WriteErrorLogs(-1, 0, GetType().FullName + "." + MethodBase.GetCurrentMethod().Name, ae.ToString());
}
catch (Exception e)
{
WriteLogs.WriteErrorLogs(-1, 0, GetType().FullName + "." + MethodBase.GetCurrentMethod().Name, e.ToString());
}
finally
{
dataLibrary.FinalizeDataService();
}
}
}
我需要为 3 个不同的数据库并行执行存储过程,但使用线程过程正在执行,但同时数据没有插入
无论我发布什么代码,我都尝试过。启动线程 使用 For 循环,因为它取决于 Connection 文件夹中存在的文件数量,因此我创建和启动的线程数量相同
答: 暂无答案
评论
DbCommand.ExecuteReaderAsync
ExecuteNonQueryAsync