我正在使用以下代码生成 CSV 文件:
private void button1_Click(object sender, EventArgs e)
{
Stopwatch swra = new Stopwatch();
swra.Start();
string NewconnectionString = connectionString;
StreamWriter CsvfileWriter = new StreamWriter(@"D:\testfile.csv");
string sqlselectQuery = "select * from tblProperty";
SqlCommand sqlcmd = new SqlCommand();
SqlConnection spContentConn = new SqlConnection(NewconnectionString);
sqlcmd.Connection = spContentConn;
sqlcmd.CommandTimeout = 0;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = sqlselectQuery;
spContentConn.Open();
using (spContentConn)
{
using (SqlDataReader sdr = sqlcmd.ExecuteReader())
using (CsvfileWriter)
{
string columnNames = Enumerable.Range(0, sdr.FieldCount).Select(i => sdr.GetName(i)).ToString();
CsvfileWriter.WriteLine(string.Join(",", columnNames));
while (sdr.Read())
//based on your Table columns you can increase and decrese columns
CsvfileWriter.WriteLine(sdr[0].ToString() + "," + sdr[1].ToString() + "," + sdr[2].ToString() + "," + sdr[3].ToString() + "," + sdr[4].ToString() + "," + sdr[5].ToString() + "," + sdr[6].ToString() + "," + sdr[7].ToString() + "," + sdr[8].ToString() + "," + sdr[9].ToString() + ",");
}
}
swra.Stop();
Console.WriteLine(swra.ElapsedMilliseconds);
}
在输出中,我得到如下屏幕截图的数据, 请帮助我。提前致谢。