1

我正在将数据从 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();
            }
        }
4

2 回答 2

0

它看起来 DataTable 对象没有被正确填充,检查这里的部分:

   for (var i = 0; i < dr2.FieldCount; i++)
    {
        _dataTable.Columns.Add("" + dr2.GetName(i) + "", typeof(string));
        _dataTable.Rows.Add("" + dr2.GetValue(i) + "");
    }

我猜你试图在这里设置数据表的列,但你不能同时追加行,你实际上将第一行的每一列值添加到不同的行中。您生成的表是这样的:

      col1        col2      col3       col4
row1  val(1,1)
row2  val(1,2)
row3  val(1,3)
row4  val(1,4)

val(x,y) 表示数据库中第 y 列和第 x 行的值。

@Darin Dimitrov 的解决方案非常适合您的问题。

于 2013-07-08T06:38:32.307 回答
-1

试着稍微简化你的代码,我的意思是那些 DataTables 和 DataSets 让我发疯:

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())
    {
        return;
    }

    using (var conn = new SQLiteConnection(ConnectionString))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT * FROM Tile1";
        using (var reader = cmd.ExecuteReader())
        using (var writer = new StreamWriter(saveFileDialog.FileName))
        {
            var columnNames = Enumerable
                .Range(0, reader.FieldCount)
                .Select(reader.GetName);
            string header = string.Join(", ", columnNames);
            writer.WriteLine(header);
            while (reader.Read())
            {
                var values = Enumerable
                    .Range(0, reader.FieldCount)
                    .Select(reader.GetValue);
                string valuesRow = string.Join(", ", values);
                writer.WriteLine(valuesRow);
            }
        }
    }
}
于 2013-07-08T06:27:47.997 回答