提问人:NiclasNilsson 提问时间:9/25/2023 更新时间:9/25/2023 访问量:22
代码不会继续使用 datatable foreach
Code will not continue with datatable foreach
问:
我有代码可以破坏 foreach 并转到顶部和另一个 foreach
它按照 batchsize 所说在数据表中运行 2 次,然后向下执行 restsharp,但之后它会转到顶部和 sql 查询之前的 foreach。
需要它对数据表中的所有行执行 batchsize 部分
我看不出我做错了什么
请帮忙
这是我的代码:
protected void run_Click1(object sender, ImageClickEventArgs e)
{
var body = "";
var evenNums = new string[3] { "2", "3", "4" };
var daytime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.GetCultureInfo("sv-SE"));
var day = DateTime.Now.ToString("yyyyMMdd", CultureInfo.GetCultureInfo("sv-SE"));
foreach (string Site in evenNums)
{
body = "{\"requestId\": \"144x25\",\"items\": [";
using (SqlConnection con = new SqlConnection())
{
string conn_str = ConfigurationManager.ConnectionStrings["SqlConnection1"].ConnectionString;
SqlConnection conn = new SqlConnection(conn_str);
string query = "SELECT [number],[surname],[forename],[emailAddress],[taxIdentifier] FROM[Customer] WITH(NOLOCK) WHERE[CustomerID] IN(SELECT[CustomerID] FROM[Visit] WITH(NOLOCK) WHERE[GamingDate] Between convert(Date, DATEADD(DAY, -365, GETDATE())) AND convert(Date, getdate())) AND(EmailAddress IS NOT NULL) AND(ContactTypes & 1 = 1) AND(HomePropertyID = " + Site + ") /* 2= Malmö 3=Göteborg 4=Stockholm */ AND(LEN(TaxIdentifier) = '12')";
SqlCommand comm = new SqlCommand(query, conn);
conn.Open();
comm.ExecuteNonQuery();
DataTable dt1 = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.Fill(dt1);
}
string csv = "";
csv += "number" + "," + "\"surname\"" + "," + "\"forename\"" + "," + "\"repsonseId\"" + "," + "\"responseTime\"";
string newFileName = "C:\\temp\\" + ort + "" + day + "FromNeon_Cleaned.csv";
string clientHeader = "number" + "," + "\"surname\"" + "," + "\"forename\"" + "," + "\"repsonseId\"" + "," + "\"responseTime\"" + Environment.NewLine;
File.WriteAllText(newFileName, clientHeader);
int batchSize = 2;
for (int j = 0; j < dt1.Rows.Count; j++)
{
if (j == batchSize) { break; }
DataRow dtRow = dt1.Rows[j];
var number = dtRow.ItemArray[0];
var surname = dtRow.ItemArray[1];
var forename = dtRow.ItemArray[2];
var emailAddress = dtRow.ItemArray[3];
string taxidentifier = (string)dtRow.ItemArray[4];
if (Personnummer.Valid(taxidentifier))
{
body += "{\"itemId\": \"" + number + "\",\"subjectId\": \"" + taxidentifier + "\"},";
}
body = body.Remove(body.Length - 1);
body += "]}";
}
var url = "https://marketing.spelpaus.se/api/marketing-subjectid/mw41yZpBFC";
var client = new RestClient(url);
var request = new RestRequest(url, Method.Post);
request.AddHeader("authorization", "vGrpkoNEWHpMuQVrIKVGfsfx2l7SLbsSfQ7PbISjMyiK545ezHW5BzsfPgJq0HA6");
request.AddHeader("Content-Type", "application/json");
request.AddBody(body, "application/json");
RestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var obj = JsonConvert.DeserializeObject<dynamic>(response.Content);
var alloweditemsdata = (obj.allowedItemIds);
var responseId = (obj.responseId);
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["number"] };
foreach (int alloweditem in alloweditemsdata)
{
DataRow Drw = dt1.Rows.Find(alloweditem);
var numberresult = Drw.ItemArray[0];
var surnameresult = Drw.ItemArray[1];
var forenameresult = Drw.ItemArray[2];
var emailAddressresult = Drw.ItemArray[3];
string taxidentifierresult = (string)Drw.ItemArray[4];
string clientDetails = " " + numberresult + ",\"" + surnameresult + "\",\"" + forenameresult + "\",\"" + emailAddressresult + "\",\"" + responseId + "\",\"" + daytime + "\"" + Environment.NewLine;
File.AppendAllText(newFileName, clientDetails, Encoding.Unicode);
}
}
}
}
}
答:
-1赞
Hezy Ziv
9/25/2023
#1
代码返回到 foreach(evenNums 中的字符串 Site)循环的顶部,这可能是因为当 j 到达 batchSize 时,您正在使用 break 打破循环。这会导致循环过早退出,并且控制返回到下一个站点的 foreach 循环的顶部。
如果希望代码处理每个站点的 DataTable 中的所有行,则应删除 break 语句。下面是删除了 break 语句的代码的相关部分:
int batchSize = 2;
for (int j = 0; j < dt1.Rows.Count; j++)
{
// Remove this 'if' condition to process all rows
// if (j == batchSize) { break; }
DataRow dtRow = dt1.Rows[j];
// Rest of your code for processing each row...
}
评论
0赞
NiclasNilsson
9/25/2023
我认为我需要休息,因为我需要从数据表中获取一些 Batchsize 是谁来控制它
下一个:提高查询速度?
评论