C# 如何将查询结果导出到文本文件并同时创建数据块?
我被分配了一个项目,其中我的需求是用来自DB2数据库的数据构建一个文本文件,到目前为止,我得到了文件所需的所有数据和布局,除了一部分。文件中的每个记录在记录的开头都由一个常量标识,前两个记录类型“a”和“B”是硬编码信息,因此这里没有来自DB的数据 例如:C# 如何将查询结果导出到文本文件并同时创建数据块?,c#,asp.net,.net,sql,visual-studio,C#,Asp.net,.net,Sql,Visual Studio,我被分配了一个项目,其中我的需求是用来自DB2数据库的数据构建一个文本文件,到目前为止,我得到了文件所需的所有数据和布局,除了一部分。文件中的每个记录在记录的开头都由一个常量标识,前两个记录类型“a”和“B”是硬编码信息,因此这里没有来自DB的数据 例如: AASNxxxxxxxxONT0010585567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx T201201190945xxxxxxxxxxxx 201201241245 B
AASNxxxxxxxxONT0010585567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
T201201190945xxxxxxxxxxxx 201201241245
B 1042 000
D16000000000000000000
文件上的下一条记录是来自数据库的一些数据,它表示一个存储编号
例如:
AASNxxxxxxxxONT0010585567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
T201201190945xxxxxxxxxxxx 201201241245
B 1042 000
D16000000000000000000
查询中的每个存储都应重复此记录
下一组记录是记录类型“B”上存储编号的纸箱,记录的数量取决于日期/装载量的多少
C1042333375563 14.300000000000Y 00000000000000000000
C1042333375564 24.100000000000Y 00000000000000000000
C1042333375768 35.000000000000Y 00000000000000000000
C1042333375769 37.980000000000Y 00000000000000000000
C1042333376321 26.120000000000Y 00000000000000000000
C1042333376322 25.860000000000Y 00000000000000000000
C1042333376323 36.000000000000Y 00000000000000000000
C1042333376324 33.000000000000Y 00000000000000000000
C1042333376325 33.000000000000Y 00000000000000000000
C1042333376326 1.000000000000Y 00000000000000000000
C1042333376327 1.000000000000Y 00000000000000000000
C1042333376328 1.000000000000Y 00000000000000000000
C1042333376329 1.000000000000Y 00000000000000000000
C1042333376330 1.000000000000Y 00000000000000000000
C1042333376331 1.000000000000Y 00000000000000000000
C1042333376332 1.000000000000Y 00000000000000000000
到目前为止,一切都按照我的要求进行。文件中的下一条记录应该是商店的纸箱总数
例如:
AASNxxxxxxxxONT0010585567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
T201201190945xxxxxxxxxxxx 201201241245
B 1042 000
D16000000000000000000
当你把所有的东西放在一起时,它应该是这样的
B 1042 000
C1042333375563 14.300000000000Y 00000000000000000000
C1042333375564 24.100000000000Y 00000000000000000000
C1042333375768 35.000000000000Y 00000000000000000000
C1042333375769 37.980000000000Y 00000000000000000000
C1042333376321 26.120000000000Y 00000000000000000000
C1042333376322 25.860000000000Y 00000000000000000000
C1042333376323 36.000000000000Y 00000000000000000000
C1042333376324 33.000000000000Y 00000000000000000000
C1042333376325 33.000000000000Y 00000000000000000000
C1042333376326 1.000000000000Y 00000000000000000000
C1042333376327 1.000000000000Y 00000000000000000000
C1042333376328 1.000000000000Y 00000000000000000000
C1042333376329 1.000000000000Y 00000000000000000000
C1042333376330 1.000000000000Y 00000000000000000000
C1042333376331 1.000000000000Y 00000000000000000000
C1042333376332 1.000000000000Y 00000000000000000000
D16000000000000000000
我希望对查询结果中的每个商店重复此操作。相反,我得到的是这个
B 1042 000
C1042333375563 14.300000000000Y 00000000000000000000
C1042333375564 24.100000000000Y 00000000000000000000
C1042333375768 35.000000000000Y 00000000000000000000
C1042333375769 37.980000000000Y 00000000000000000000
C1042333376321 26.120000000000Y 00000000000000000000
C1042333376322 25.860000000000Y 00000000000000000000
C1042333376323 36.000000000000Y 00000000000000000000
C1042333376324 33.000000000000Y 00000000000000000000
C1042333376325 33.000000000000Y 00000000000000000000
C1042333376326 1.000000000000Y 00000000000000000000
C1042333376327 1.000000000000Y 00000000000000000000
C1042333376328 1.000000000000Y 00000000000000000000
C1042333376329 1.000000000000Y 00000000000000000000
C1042333376330 1.000000000000Y 00000000000000000000
C1042333376331 1.000000000000Y 00000000000000000000
C1042333376332 1.000000000000Y 00000000000000000000
C1084333362820 15.940000000000Y 00000000000000000000
C1084333362821 25.020000000000Y 00000000000000000000
C1084333363628 34.120000000000Y 00000000000000000000
C1084333363629 30.760000000000Y 00000000000000000000
C1084333363833 5.860000000000Y 00000000000000000000
C1084333363834 25.900000000000Y 00000000000000000000
C1084333363835 25.760000000000Y 00000000000000000000
C1084333363836 36.000000000000Y 00000000000000000000
C1084333363837 33.000000000000Y 00000000000000000000
C1084333363838 33.000000000000Y 00000000000000000000
C1084333363839 1.000000000000Y 00000000000000000000
C1084333363840 1.000000000000Y 00000000000000000000
C1084333363841 1.000000000000Y 00000000000000000000
C1084333363842 1.000000000000Y 00000000000000000000
C1114333360163 30.740000000000Y 00000000000000000000
C1114333360164 29.000000000000Y 00000000000000000000
C1114333360165 31.540000000000Y 00000000000000000000
C1114333360166 39.780000000000Y 00000000000000000000
C1114333360167 33.680000000000Y 00000000000000000000
C1114333360168 24.780000000000Y 00000000000000000000
C1114333360169 29.860000000000Y 00000000000000000000
C1114333360706 15.960000000000Y 00000000000000000000
C1114333361051 25.680000000000Y 00000000000000000000
C1114333361052 1.000000000000Y 00000000000000000000
C1114333361053 1.000000000000Y 00000000000000000000
C1114333361054 1.000000000000Y 00000000000000000000
C1114333361055 1.000000000000Y 00000000000000000000
C1118333382930 27.060000000000Y 00000000000000000000
C1118333382931 20.560000000000Y 00000000000000000000
C1118333383074 25.180000000000Y 00000000000000000000
C1118333383075 30.420000000000Y 00000000000000000000
C1118333383077 28.420000000000Y 00000000000000000000
C1118333383406 25.980000000000Y 00000000000000000000
C1118333383407 26.020000000000Y 00000000000000000000
C1118333383408 25.760000000000Y 00000000000000000000
C1118333383409 36.000000000000Y 00000000000000000000
C1118333383410 36.000000000000Y 00000000000000000000
C1118333383411 33.000000000000Y 00000000000000000000
C1118333383412 33.000000000000Y 00000000000000000000
C1118333383413 1.000000000000Y 00000000000000000000
C1118333383414 1.000000000000Y 00000000000000000000
C1118333383415 1.000000000000Y 00000000000000000000
C1118333383416 1.000000000000Y 00000000000000000000
C1118333383417 1.000000000000Y 00000000000000000000
C1118333383418 1.000000000000Y 00000000000000000000
C1118333383419 1.000000000000Y 00000000000000000000
C1136333360998 22.540000000000Y 00000000000000000000
C1136333361237 31.780000000000Y 00000000000000000000
C1136333361238 31.260000000000Y 00000000000000000000
C1136333361689 26.060000000000Y 00000000000000000000
C1136333361690 25.880000000000Y 00000000000000000000
C1136333361691 36.000000000000Y 00000000000000000000
C1136333361692 1.000000000000Y 00000000000000000000
C1136333361693 1.000000000000Y 00000000000000000000
C1136333361694 1.000000000000Y 00000000000000000000
C1136333361695 1.000000000000Y 00000000000000000000
C1136333361696 1.000000000000Y 00000000000000000000
C1136333361697 1.000000000000Y 00000000000000000000
C1136333361698 1.000000000000Y 00000000000000000000
C1289333366296 20.080000000000Y 00000000000000000000
C1289333366917 26.920000000000Y 00000000000000000000
C1289333366918 31.120000000000Y 00000000000000000000
C1289333366919 36.780000000000Y 00000000000000000000
C1289333366920 46.860000000000Y 00000000000000000000
C1289333366921 31.700000000000Y 00000000000000000000
C1289333366922 31.420000000000Y 00000000000000000000
C1289333367268 26.000000000000Y 00000000000000000000
C1289333367269 25.820000000000Y 00000000000000000000
C1289333367270 36.000000000000Y 00000000000000000000
C1289333367271 33.000000000000Y 00000000000000000000
C1289333367272 33.000000000000Y 00000000000000000000
C1289333367273 1.000000000000Y 00000000000000000000
C1289333367274 1.000000000000Y 00000000000000000000
C1289333367275 1.000000000000Y 00000000000000000000
C1289333367276 1.000000000000Y 00000000000000000000
C1519333340832 19.660000000000Y 00000000000000000000
C1519333340833 19.840000000000Y 00000000000000000000
C1519333341219 35.540000000000Y 00000000000000000000
C1519333341220 33.640000000000Y 00000000000000000000
C1519333341221 29.360000000000Y 00000000000000000000
C1519333341222 28.520000000000Y 00000000000000000000
C1519333341223 30.420000000000Y 00000000000000000000
C1519333341224 33.340000000000Y 00000000000000000000
C1519333341225 27.220000000000Y 00000000000000000000
C1519333341226 31.640000000000Y 00000000000000000000
C1519333341227 30.680000000000Y 00000000000000000000
C1519333341228 29.400000000000Y 00000000000000000000
C1519333341558 26.040000000000Y 00000000000000000000
C1519333341559 24.900000000000Y 00000000000000000000
C1519333341560 36.000000000000Y 00000000000000000000
C1519333341561 1.000000000000Y 00000000000000000000
C1519333341562 1.000000000000Y 00000000000000000000
C1519333341563 1.000000000000Y 00000000000000000000
C1519333341564 1.000000000000Y 00000000000000000000
C1519333341565 1.000000000000Y 00000000000000000000
C1519333341566 1.000000000000Y 00000000000000000000
C1519333341567 1.000000000000Y 00000000000000000000
C1523333350211 30.080000000000Y 00000000000000000000
C1523333350212 47.420000000000Y 00000000000000000000
C1523333350213 35.480000000000Y 00000000000000000000
C1523333350214 33.640000000000Y 00000000000000000000
C1523333350215 30.760000000000Y 00000000000000000000
C1523333350216 38.140000000000Y 00000000000000000000
C1523333350219 33.400000000000Y 00000000000000000000
C1523333350268 20.380000000000Y 00000000000000000000
C1523333350269 8.820000000000Y 00000000000000000000
C1523333350270 23.940000000000Y 00000000000000000000
C1523333350271 20.580000000000Y 00000000000000000000
C1523333350646 26.020000000000Y 00000000000000000000
C1523333350647 26.120000000000Y 00000000000000000000
C1523333350648 25.800000000000Y 00000000000000000000
C1523333350649 36.000000000000Y 00000000000000000000
C1523333350650 36.000000000000Y 00000000000000000000
C1523333350651 33.000000000000Y 00000000000000000000
C1523333350652 33.000000000000Y 00000000000000000000
C1523333350653 1.000000000000Y 00000000000000000000
C1523333350654 1.000000000000Y 00000000000000000000
C1523333350655 1.000000000000Y 00000000000000000000
C1523333350656 1.000000000000Y 00000000000000000000
C1523333350657 1.000000000000Y 00000000000000000000
C1523333350658 1.000000000000Y 00000000000000000000
C1523333350659 1.000000000000Y 00000000000000000000
D16000000000000000000
正如您所看到的,只有一个记录类型“B”和记录类型“D”,而我应该为每个存储区设置一个记录类型。存储编号是记录类型“C”字符后的前4位数字
下面是一个示例,说明了在多个商店中它应该是什么样子:
AASNxxxxxxxxONT0010585567 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
T201201190945xxxxxxxxxxxx 201201241245
B 1042 000
C1042333375563 14.300000000000Y 00000000000000000000
C1042333375564 24.100000000000Y 00000000000000000000
C1042333375768 35.000000000000Y 00000000000000000000
C1042333375769 37.980000000000Y 00000000000000000000
C1042333376321 26.120000000000Y 00000000000000000000
C1042333376322 25.860000000000Y 00000000000000000000
C1042333376323 36.000000000000Y 00000000000000000000
C1042333376324 33.000000000000Y 00000000000000000000
C1042333376325 33.000000000000Y 00000000000000000000
C1042333376326 1.000000000000Y 00000000000000000000
C1042333376327 1.000000000000Y 00000000000000000000
C1042333376328 1.000000000000Y 00000000000000000000
C1042333376329 1.000000000000Y 00000000000000000000
C1042333376330 1.000000000000Y 00000000000000000000
C1042333376331 1.000000000000Y 00000000000000000000
C1042333376332 1.000000000000Y 00000000000000000000
D16000000000000000000
B 1084 000
C1084333362820 15.940000000000Y 00000000000000000000
C1084333362821 25.020000000000Y 00000000000000000000
C1084333363628 34.120000000000Y 00000000000000000000
C1084333363629 30.760000000000Y 00000000000000000000
C1084333363833 5.860000000000Y 00000000000000000000
C1084333363834 25.900000000000Y 00000000000000000000
C1084333363835 25.760000000000Y 00000000000000000000
C1084333363836 36.000000000000Y 00000000000000000000
C1084333363837 33.000000000000Y 00000000000000000000
C1084333363838 33.000000000000Y 00000000000000000000
C1084333363839 1.000000000000Y 00000000000000000000
C1084333363840 1.000000000000Y 00000000000000000000
C1084333363841 1.000000000000Y 00000000000000000000
C1084333363842 1.000000000000Y 00000000000000000000
D14000000000000000000
我怎样才能做到这一点?这可能吗?我肯定是的,但我就是搞不懂。这是我的密码:
//Declare the ConnectionString
odbccon.ConnectionString = ConfigurationManager.AppSettings["PkMS"];
//Create Physical File
w = File.CreateText("C:\\Users\\jorge.mejia\\Documents\\ThirdParty_ASN\\Test Files\\xxxxASN_" + DateTime.Now.ToString("HHmmss") + ".txt");
//Create Header Record - Type "A"
w.WriteLine(constantA + fileidentifier + clientidentifier + primaryscac + dcidentifier + shipmentID + masterBOL + buildingID);
//Create Load Record - Type "T"
w.WriteLine(constantT + trailerclosedate + trailerclosetime + trailernumber + sealnumber + expectedarrivaldate + expectedarrivaltime + trailerserialnumber + carrier);
odbccon.Open();
OdbcCommand getstore = new OdbcCommand("select count(*) as totalstorecartons, chstor from wm242basd.chcart00 where chldno = '0010585567' group by chstor order by chstor", odbccon);
OdbcDataReader odbcr2 = getstore.ExecuteReader();
while (odbcr2.Read())
{
storenumber = odbcr2["chstor"].ToString();
totalstorecartons = odbcr2["totalstorecartons"].ToString();
//Create Store Record - Type "B"
w.WriteLine(constantB + deliverycarrierSCAC + clientroute + businesscode + storenumber + clientbolnumber + terminalcode + deliverydays + deliverystarttime + deliveryendtime + carrierpro + expecteddeliverydate);
OdbcCommand getcartons = new OdbcCommand("select chcasn, chstor, chacwt from wm242basd.chcart00 where chldno = '0010585567' order by chstor", odbccon);
OdbcDataReader odbcr = getcartons.ExecuteReader();
while (odbcr.Read())
{
cartonnumber = odbcr["chcasn"].ToString() + " ";
storenumber = odbcr["chstor"].ToString() + " ";
weightlbs = odbcr["chacwt"].ToString();
//Create Store Record - Type "C"
w.WriteLine(constantC + cartonnumber + weightlbs + cubicfeet + conveyable + specialhandling + cartonvalue + cartonretail + units + palletnumber);
}
//Create Store Record - Type "D"
w.WriteLine(constantD + totalstorecartons + totalstoreweight + totalstorecubicfeet);
}
OdbcCommand getloadtotal = new OdbcCommand("select count(*) as totaltrailercartons, sum(chacwt) as totaltrailerweight from wm242basd.chcart00 where chldno = '0010585567'", odbccon);
OdbcDataReader odbcr3 = getloadtotal.ExecuteReader();
while (odbcr3.Read())
{
totaltrailercartons = odbcr3["totaltrailercartons"].ToString();
totaltrailerweight = odbcr3["totaltrailerweight"].ToString();
}
//Create Store Record - Type "E"
w.WriteLine(constantE + totaltrailercartons + totaltrailerweight + totaltrailercubicfeet);
//Maintenance and Close
w.Flush();
w.Close();
您能提供的任何帮助都将不胜感激。您似乎有两个问题。第一,你只经历了一次外环我不知道为什么,但我可以帮你解决第二个问题。第二个问题是,内部循环的查询一次需要限制为一组数据 在getcartons查询中:
从wm242basd.chcart00中选择chcasn、chstor、chacwt,其中chldno='0010585567'按chstor下单
您应该有一个条件,将结果限制为当前门店号,而不是再次按门店号下单。我不熟悉Odbc,但对于Oracle,我会这样做:
while (odbcr2.Read())
{
storenumber = odbcr2["chstor"].ToString();
totalstorecartons = odbcr2["totalstorecartons"].ToString();
//Create Store Record - Type "B"
w.WriteLine(constantB + deliverycarrierSCAC + clientroute + businesscode + storenumber + clientbolnumber + terminalcode + deliverydays + deliverystarttime + deliveryendtime + carrierpro + expecteddeliverydate);
OdbcCommand getcartons = new OdbcCommand("select chcasn, chstor, chacwt from wm242basd.chcart00 where chldno = '0010585567' and chstor = :storeNumber", odbccon);
getcartons.AddParameter("storeNumber", storenumber);
OdbcDataReader odbcr = getcartons.ExecuteReader();
while (odbcr.Read())
{
cartonnumber = odbcr["chcasn"].ToString() + " ";
storenumber = odbcr["chstor"].ToString() + " ";
weightlbs = odbcr["chacwt"].ToString();
//Create Store Record - Type "C"
w.WriteLine(constantC + cartonnumber + weightlbs + cubicfeet + conveyable + specialhandling + cartonvalue + cartonretail + units + palletnumber);
}
//Create Store Record - Type "D"
w.WriteLine(constantD + totalstorecartons + totalstoreweight + totalstorecubicfeet);
}
请注意,更改的两行是:
OdbcCommand getcartons = new OdbcCommand("select chcasn, chstor, chacwt from wm242basd.chcart00 where chldno = '0010585567' and chstor = :storeNumber", odbccon);
getcartons.AddParameter("storeNumber", storenumber);
另外,如果您正在使用VisualStudio,并且可以从本地计算机连接到DB,那么这将很容易调试。只需在每个循环中放置一个断点,然后查看查询结果。您确实需要缩短问题中的示例。如果没有大量的滚动,你不可能完全阅读这个问题。作为一个做大量数据导入的人,如果有人给我这样的文件来导入,我会向他们开枪。在我花很多时间在这方面之前,我会确保需要这些信息的人真的更愿意以这种格式获取这些信息。有时,提出这些要求的人既没有与信息的提供者也没有与信息的接收者交谈,以确保这是最好的格式。(如果是大型机,它可能是)我们将文件返回给客户机,并以更好的格式请求,他们通常会说“哦,好吧,我们不知道这会更容易”。我已经这样做了,事实上以前情况更糟,是的,这将是大型机。