c# - 如何将数据表拆分为多个表并增加 1 分钟延迟
问题描述
我知道有很多与它相关的问题和解决方案。但我没有得到它们。我有一个记录数据表,8000
将来会增加。我想把它分成1000
每个表的记录,然后想增加一分钟的延迟。下面是我的代码
DataTable dt = util.getReading();
if (dt != null && dt.Rows.Count > 0)
{
totalRec = dt.Rows.Count;
string ReqEnvPath = System.Configuration.ConfigurationManager.AppSettings["ReadEnvPath"].ToString();
XElement SoapReqEnv = XElement.Load(ReqEnvPath);
foreach (DataRow dr in dt.Rows)
{
//string uniqueID = dr["UNIQ_KEY"].ToString();
string uniqueID = dr["uniqueid"].ToString();
string meterNo = dr["msn"].ToString();
//APPLICATION_NO, REF_NO, XMETER_NO, METER_SERIAL_NO, KWH_METER_STATUS
string timestamp = DateTime.UtcNow.ToString("o");
StringBuilder sbArg0 = new StringBuilder();
try
{
sbArg0.AppendFormat(@"<?xml version=""1.0"" encoding=""UTF-8"" ?> " + SoapReqEnv.ToString(), uniqueID, startTS, endTS, timestamp, this.HEXURL, this.HEXUID, this.HEXPWD);
Guid currentGuid = Guid.NewGuid();
obj.getResponseAsync(sbArg0.ToString(), currentGuid + "$" + uniqueID);
obj.getResponseCompleted += this.myHandler;
string delayMS = System.Configuration.ConfigurationManager.AppSettings["DelayMS"].ToString();
ushort delay = 1000;
ushort.TryParse(delayMS, out delay);
System.Threading.Thread.Sleep(delay);
}
catch (Exception ex)
{
error += "Error for UniqID:" + uniqueID + "Desc:" + ex.Message + "\n";
}
finally
{
//System.Threading.Thread.CurrentThread.Join();
}
}
}
如何1000
在添加 1 分钟延迟时间的同时将数据表拆分为每行,以便在 1 分钟延迟后获取接下来的 1000 条记录?
更新 1
从测试的角度来看,我记录了 20 条记录,并按照此解决方案将数据表拆分为 5 条。
private List<DataTable> SplitTable(DataTable originalTable, int batchSize)
{
List<DataTable> tables = new List<DataTable>();
int i = 0;
int j = 1;
DataTable newDt = originalTable.Clone();
newDt.TableName = "Table_" + j;
newDt.Clear();
foreach (DataRow row in originalTable.Rows)
{
DataRow newRow = newDt.NewRow();
newRow.ItemArray = row.ItemArray;
newDt.Rows.Add(newRow);
i++;
if (i == batchSize)
{
tables.Add(newDt);
j++;
newDt = originalTable.Clone();
newDt.TableName = "Table_" + j;
newDt.Clear();
i = 0;
}
}
return tables;
}
public string LoadAMIReadings()
{
string[] arr = new string[]
{"37030298060","28373341367200U","002997004330","002997004330",
"37010674330","28371551110400U","002997006388","002997006388",
"37030315632","28373131369800U","002998000563","002998000563",
"37010681112","28374211369900U","002998000938","002998000938",
"37010682305","28374331368400U","002998000351","002998000351",
"37010682312","28374331369600U","002998000995","002998000995",
"37030297517","28373321004010U","002998003915","002998003915",
"37010674134","28371550292110U","002997006486","002997006486",
"37030295965","28373150875200U","002997004697","002997004697",
"37010678805","28372720047060U","002997002511","002997002511",
"37010675029","28372230024431U","002999000385","002999000385",
"37030299221","28373430506600U","002997000337","002997000337",
"37030299227","28373430507200U","002997000382","002997000382",
"37030297870","28373340570200U","002997001558","002997001558",
"37010679004","28372730053742U","002997001334","002997001334",
"37010719967","28372810024580U","002997006816","002997006816",
"37010720185","28374120091810U","002998003930","002998003930",
"37010720008","28372810036450U","002997006911","002997006911",
"37010680399","20374131467200U","002998002734","002998002734",
"37030296089","28373151133100U","002997002578","002997002578"};
DataTable dt = new DataTable();
dt.Columns.Add("Application_No", typeof(string));
dt.Columns.Add("REF_NO", typeof(string));
dt.Columns.Add("METER_SERIAL_NO", typeof(string));
dt.Columns.Add("XMETER_NO", typeof(string));
dt.FillDataTable(arr);
if (dt != null && dt.Rows.Count > 0)
{
totalRec = dt.Rows.Count;
string ReqEnvPath = System.Configuration.ConfigurationManager.AppSettings["ReadEnvPath"].ToString();
XElement SoapReqEnv = XElement.Load(ReqEnvPath);
List<DataTable> splitdt = SplitTable(dt, 5);
foreach (DataRow dr in dt.Rows) // here I want to pass splitdt but it gives me error
{
//string uniqueID = dr["UNIQ_KEY"].ToString();
string uniqueID = dr["Application_No"].ToString();
string meterNo = dr["METER_SERIAL_NO"].ToString();
//APPLICATION_NO, REF_NO, XMETER_NO, METER_SERIAL_NO, KWH_METER_STATUS
string timestamp = DateTime.UtcNow.ToString("o");
StringBuilder sbArg0 = new StringBuilder();
try
{
sbArg0.AppendFormat(@"<?xml version=""1.0"" encoding=""UTF-8"" ?> " + SoapReqEnv.ToString(), uniqueID, startTS, endTS, timestamp, this.HEXURL, this.HEXUID, this.HEXPWD);
Guid currentGuid = Guid.NewGuid();
obj.getResponseAsync(sbArg0.ToString(), currentGuid + "$" + uniqueID);
obj.getResponseCompleted += this.myHandler;
string delayMS = System.Configuration.ConfigurationManager.AppSettings["DelayMS"].ToString();
ushort delay = 1000;
ushort.TryParse(delayMS, out delay);
System.Threading.Thread.Sleep(delay);
}
catch (Exception ex)
{
error += "Error for UniqID:" + uniqueID + "Desc:" + ex.Message + "\n";
}
finally
{
//System.Threading.Thread.CurrentThread.Join();
}
}
}
}
现在我有 4 个表,每 5 行。我想从第一个表进程中获取数据,然后等待 1 分钟,然后获取第二个拆分表,依此类推。
我试图splitdt
进入foreach(DataRow in splitdt.Rows)
,但它给了我一个错误。
我怎样才能实现它?
任何帮助将不胜感激
解决方案
我希望这是您想要实现的目标:
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
...
public void Test()
{
DataTable bigDataTable = GetBigDataTable();
var splitedTables = new List<DataTable>();
var smallTable = new DataTable();
int page = 0;
int pageSize = 1000;
List<DataRow> results;
while (true)
{
results = bigDataTable.AsEnumerable().Skip(pageSize * page++).Take(pageSize).ToList();
if (!results.Any())
break;
smallTable = results.CopyToDataTable();
splitedTables.Add(smallTable);
Thread.Sleep(1000 * 60 * 1);
} while (results.Any());
}
为此,您需要添加对System.Data.DataSetExtensions的引用。
这也是SO中的类似问题。
推荐阅读
- boost - 在配置脚本/Autotools 中设置配置选项,并将 Boost 安装在不寻常的位置(EPEL 替代 boost 安装)
- mysql - 如何在 bash 脚本中使用 SQL 更新功能
- php - 无法使用 domcrawler 提取 og:image 但可以使用 og:description
- java - Java XML如何获取一个元素的parents和parents属性?
- flutter - 在颤动中淡入背景图像
- next.js - 无法确定 Next.js 的正确配置,使用 Apollo/GraphQL-Yoga 进行上传和订阅
- javascript - 文件夹获取新项目时如何更新页面而不重新加载?
- node.js - 没有调用 DeserializeUser 并且没有使用 express 和 passportJS 将 cookie 发送到客户端
- fasm - 为什么仅使用符号标志不能进行带符号的数字比较
- python - 使用 Opencv 和 Python 训练 SVM 和预测