在 C# 中,我如何才能导出到文件具有多行的字符串生成器,维护这些行。这是我的代码:
StringBuilder builder = new StringBuilder();
using (SqlCommand command = new SqlCommand(consulta, SqlConn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader != null && reader.Read())
{
builder.AppendFormat("{0,-5} {1,-10} {2,-10} {3,-18} {4,-10} {5,-2} {6,-10} {7,-5} {8,-10}", Convert.ToString(reader[0]), reader.GetDateTime(1).ToString("yyyy-MM-dd"), reader.GetDateTime(2).ToString("yyyy-MM-dd"), Convert.ToString(reader[3]), Convert.ToString(reader[4]), "06", Convert.ToString(reader[5]), Convert.ToString(reader[6]), Convert.ToString(reader[7]));
}
}
}
这是我保存文件的地方,但它不尊重这些行。
File.WriteAllText(saveFileDialog1.FileName, builder.ToString());