如何将查询结果导出到具有多条记录的文本文件中
本文关键字:记录 文本 文件 查询 结果 | 更新日期: 2023-09-27 18:20:48
我正试图将以下查询中的某些字段导出到Text文件中。我面临的问题是,当我导出文本文件时,文本文件中只显示了一条记录,我还想不向该文本文件附加任何查询结果。
此外,有些线路不需要重复,
odbccon.Open();
OdbcCommand getcartons = new OdbcCommand("select * from wm242basd.chcart00 where chldno = '0010585567'", odbccon);
OdbcDataAdapter adp = new OdbcDataAdapter(getcartons);
OdbcDataReader odbcr = getcartons.ExecuteReader();
while (odbcr.Read())
{
cartonnumber = odbcr["chcasn"].ToString() + " ";
storenumber = odbcr["chstor"].ToString() + " ";
weightlbs = odbcr["chacwt"].ToString();
}
#region
//Assign values to each String for Header Record - Type "A"
constantA = "A";
fileidentifier = "ASN";
clientidentifier = "1111"; //Currently Pending on Call from Friday
primaryscac = "1111"; //Currently Pending on Call from Friday
dcidentifier = "ONT";
shipmentID = "0010585567 "; //This String could come from DataBase OR could be populated from TextBox by User
masterBOL = " "; //This String could come from DataBase
buildingID = " "; //Currently Pending on Call from Friday
//Create Header Record - Type "A"
w.WriteLine(constantA + fileidentifier + clientidentifier + primaryscac + dcidentifier + shipmentID + masterBOL + buildingID);
//Assign values to each String for Load Record - Type "T"
constantT = "T";
trailerclosedate = DateTime.Now.ToString("yyyyMMdd");
trailerclosetime = DateTime.Now.ToString("HHmm");
trailernumber = " "; //Pending on App Accessability OR could be populated from TextBox by user
sealnumber = " ";
expectedarrivaldate = DateTime.Now.AddDays(5).ToString("yyyyMMdd");
expectedarrivaltime = DateTime.Now.AddHours(3).ToString("HHmm");
trailerserialnumber = " ";
carrier = " ";
//Create Load Record - Type "T"
w.WriteLine(constantT + trailerclosedate + trailerclosetime + trailernumber + sealnumber + expectedarrivaldate + expectedarrivaltime + trailerserialnumber + carrier);
//Assign values to each String for Store Record - Type "B"
constantB = "B";
deliverycarrierSCAC = " ";
clientroute = " ";
businesscode = " ";
//storenumber = "1001 "; //This String should come from the DataBase
clientbolnumber = " ";
terminalcode = " ";
deliverydays = "000";
deliverystarttime = " ";
deliveryendtime = " ";
carrierpro = " ";
expecteddeliverydate = " ";
//Create Store Record - Type "B"
w.WriteLine(constantB + deliverycarrierSCAC + clientroute + businesscode + storenumber + clientbolnumber + terminalcode + deliverydays + deliverystarttime + deliveryendtime + carrierpro + expecteddeliverydate);
//Assign values to each String for Store Record - Type "C"
constantC = "C";
//cartonnumber = "1002333323569 "; //This String should come from DataBase
//weightlbs = "0000000"; //This String could also come from DataBase
cubicfeet = "0000000";
conveyable = "Y";
specialhandling = " ";
cartonvalue = "00000000";
cartonretail = "00000000";
units = "0000"; //This String could also come from DataBase
palletnumber = " "; //This String could also come from DataBase
//Create Store Record - Type "C"
w.WriteLine(constantC + cartonnumber + weightlbs + cubicfeet + conveyable + specialhandling + cartonvalue + cartonretail + units + palletnumber);
//Assign values to each String for Store Record - Type "D"
constantD = "D";
totalstorecartons = "0000023"; //This String should come from DataBase
totalstoreweight = "000000000"; //This String could come from DataBase
totalstorecubicfeet = "000000000";
//Create Store Record - Type "D"
w.WriteLine(constantD + totalstorecartons + totalstoreweight + totalstorecubicfeet);
//Assign values to each String for Store Record - Type "E"
constantE = "E";
totaltrailercartons = "0000023"; //This String should come from DataBase
totaltrailerweight = "000000000"; //This String could come from DataBase
totaltrailercubicfeet = "000000000";
//Create Store Record - Type "E"
w.WriteLine(constantE + totaltrailercartons + totaltrailerweight + totaltrailercubicfeet);
#endregion
//Maintenance and Close
w.Flush();
w.Close();
}
while循环重复设置这三个变量,但一旦循环完成,每个变量只有一个值。
因此,在写入文件时,将使用上一条记录中的值,并且仅使用这些值。
移动while循环的右大括号,使其包含您希望为每个数据记录执行的所有代码,但请确保文件在while循环之前打开并在其后关闭