我有一個文本文件,顯示從sql數據庫檢索到的信息。有沒有一種方法可以在將用作標題的數據之上插入一行?將標題行插入文本文件? C#
以下是文本文件部分的代碼。
StringBuilder sb = new StringBuilder();
foreach (DataRow row in dataTable.Rows)
{
sb.AppendLine(row[0].ToString().PadRight(8, ' ') + "," + row[1].ToString().PadRight(4, ' ') + "," + row[2].ToString().PadRight(8, ' ') + "," + row[3].ToString().PadRight(1, ' ')
+ "," + row[4].ToString().PadRight(30, ' ') + "," + row[5].ToString().PadRight(15, ' ') + "," + row[6].ToString().PadRight(1, ' ') + "," + row[7].ToString().PadRight(4, ' ')
+ "," + row[8].ToString().PadRight(30, ' ') + "," + row[9].ToString().PadRight(6, ' ') + "," + row[10].ToString().PadRight(30, ' ') + "," + row[11].ToString().PadRight(30, ' ')
+ "," + row[12].ToString().PadRight(30, ' ') + "," + row[13].ToString().PadRight(30, ' ') + "," + row[14].ToString().PadRight(30, ' ') + "," + row[15].ToString().PadRight(13, ' ')
+ "," + row[16].ToString().PadRight(13, ' ') + "," + row[17].ToString().PadRight(13, ' ') + "," + row[18].ToString().PadRight(13, ' ') + "," + row[19].ToString().PadRight(7, ' ')
+ "," + row[20].ToString().PadRight(1, ' ') + "," + row[21].ToString().PadRight(11, ' ') + "," + row[22].ToString().PadRight(1, ' ') + "," + row[23].ToString().PadRight(13, ' ')
+ "," + row[24].ToString().PadRight(8, ' ') + "," + row[25].ToString().PadRight(39, ' '));
}
using (StreamWriter outFile = new StreamWriter(mypath + @"\CCC.txt")) outFile.Write(sb.ToString());
myConnection.Close();
與輸出行相同的方式,您可以在**'foreach'之前輸出標題**,如果這是問題。 – Sinatr