我正在将数据从 SQLite 导出到.csv
,但我在行上遇到问题,为什么结果总是出现在第一行?我错过了什么??
结果:
我的代码:
private void DataExport()
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Title = "Choose file to save to",
FileName = ".csv",
Filter = "CSV (*.csv)|*.csv",
FilterIndex = 0,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
if (saveFileDialog.ShowDialog() == true)
{
SQLiteCommand cmd = new SQLiteCommand();
String sSQL;
sSQL = "select * from Tile1";
cmd.CommandText = sSQL;
clsCon.con.Open();
cmd.Connection = clsCon.con;
SQLiteDataReader dr2;
dr2 = cmd.ExecuteReader();
dr2.Read();
DataSet _dataSet = new DataSet();
DataTable _dataTable = new DataTable();
for (var i = 0; i < dr2.FieldCount; i++)
{
_dataTable.Columns.Add("" + dr2.GetName(i) + "", typeof(string));
_dataTable.Rows.Add("" + dr2.GetValue(i) + "");
}
var rows = _dataTable.Rows;
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = _dataTable.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(saveFileDialog.FileName, sb.ToString());
clsCon.con.Close();
}
}