通过行选择将数据库(.mdb/.accdb)导出到.csv [英] Export DataBase (.mdb / .accdb) to .csv with row selection

查看:238
本文介绍了通过行选择将数据库(.mdb/.accdb)导出到.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做什么:

我需要加载数据库,搜索条目并导出选择的列.

I Need to load a Database, search entries and Export the selected columns.

问题:

我得到一个DataGrid(没有DataGridView)作为列表,并选择了数据库条目,但我无法获得任何解决方案来仅导出以;作为分隔符的所选行.

I got a DataGrid (no DataGridView) for list and select the Database entries and I cant get any solution to Export only the selected rows with ; as seperator.

编码我如何加载和列出数据库

using (OleDbConnection ODC = new OleDbConnection("Provider = Microsoft.Jet.OLEDB." + iOledDBVersion + "; Data Source = " + connectionString))
{
    OleDbCommand ODCmd = new OleDbCommand(insertSQL);

    ODCmd.Connection = ODC;
    ODC.Open();
    ODCmd.ExecuteNonQuery();

    //Data-Adapter erstellen
    OleDbDataAdapter OleDbDataAdapter_Temp = new OleDbDataAdapter(insertSQL, ODC);
    OleDbCommandBuilder OleDbCommandBuilder_Temp = new OleDbCommandBuilder(OleDbDataAdapter_Temp);


    //Daten aus der Datenbank in Dataset speichern
    OleDbDataAdapter_Temp.Fill(DataSet_DB, sTabelle);

    // Schleife für jede Tabelle
    for (int i = 0; i < DataSet_DB.Tables.Count; i++)
    {
        // Setzt die Spalten der DB in das DataGrid
        DataGrid_Table.DataContext = DataSet_DB.Tables[i].DefaultView;
    }
}

是否存在导出这些行的正确方法?

推荐答案

有一个SelectedItems属性,该属性将返回DataGrid中的所选项目.在您的情况下,您可以将每个这样的项目都添加到DataRowView,例如:

There is a SelectedItems property that will return the selected items in the DataGrid. In your case you could each such item to a DataRowView, e.g.:

StringBuilder sb = new StringBuilder();
foreach(var selectedRow in DataGrid_Table.SelectedItems.OfType<DataRowView>())
{
    foreach(DataColumn column in selectedRow.DataView.Table.Columns)
    {
        sb.Append(selectedRow[column.ColumnName] + ";");
    }
    sb.Append(Environment.NewLine);
}

string export = sb.ToString();

这篇关于通过行选择将数据库(.mdb/.accdb)导出到.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆