C# 使用C从Access表创建.csv文件

C# 使用C从Access表创建.csv文件,c#,csv,ms-access-2003,C#,Csv,Ms Access 2003,我正在尝试编写一个程序,从Access表创建一个.csv文件。我不知道该怎么做,我在研究时发现的一切都是相反的,从.csv文件创建一个访问表 到目前为止,我已经创建了一个windows窗体应用程序,允许用户选择访问目录.mdb的数据路径,然后在按下Go按钮后,目录中的表列表显示在列表框中 接下来我需要做的是,允许用户选择其中一个表,然后从所选表创建一个.csv文件,我不知道如何做。以下是我目前的代码: using System; using System.Collections.Generic;

我正在尝试编写一个程序,从Access表创建一个.csv文件。我不知道该怎么做,我在研究时发现的一切都是相反的,从.csv文件创建一个访问表

到目前为止,我已经创建了一个windows窗体应用程序,允许用户选择访问目录.mdb的数据路径,然后在按下Go按钮后,目录中的表列表显示在列表框中

接下来我需要做的是,允许用户选择其中一个表,然后从所选表创建一个.csv文件,我不知道如何做。以下是我目前的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;

namespace TranslatorHelper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void btnPath_Click(object sender, EventArgs e)
    {
        string dialogText = "";
        // Show the dialog and get result.
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            dialogText = openFileDialog1.ToString();
            dialogText = dialogText.Replace("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", "");
            txtDataPath.Text = dialogText;
        }
    }

    private void btnGetTables_Click(object sender, EventArgs e)
    {
        // Microsoft Access provider factory
        DbProviderFactory factory =
        DbProviderFactories.GetFactory("System.Data.OleDb");

        DataTable userTables = null;

        using (DbConnection connection = factory.CreateConnection())
        {
            connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+txtDataPath.Text;

            // We only want user tables, not system tables
            string[] restrictions = new string[4];
            restrictions[3] = "Table";

            connection.Open();

            // Get list of user tables
            userTables = connection.GetSchema("Tables", restrictions);
        }

    // Add list of table names to listBox
    for (int i = 0; i < userTables.Rows.Count; i++)
    lstTables.Items.Add(userTables.Rows[i][2].ToString());

    }

    private void lstTables_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

请尽你所能提供任何建议,我非常感激,我真的被困在这里。

也许我可以把你推到正确的方向:

您有了开始,即DbConnection。然后,您可以使用以下命令创建DbCommand:

DbCommand command = connection.CreateCommand();
从那里,您可以指定文本,例如:

command.CommandText = "SELECT * FROM " + tableName;  //you might have to wrap the table name with quotes or square brackets!
然后你可以得到一个阅读器:

DbDataReader reader = command.ExecuteReader(CommandBehavior.Default);
一旦你有了读卡器,你就有了数据,这意味着你可以:

DataTable dt = new DataTable("MyNewTable");
dt.Load(reader);
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn dc in row.Columns)
    {
        if (!String.IsNullOrEmpty(sb.ToString()))
            sb.Append(",");
        sb.Append(row[dc.ColumnName].ToString());
    }
    sb.Append("\n");
}
要获取列表框选择,可以使用:

lstTables.SelectedItem.ToString()
这将为您提供该项,或者在您的情况下提供一个表名。不要忘记,某些单词在SQL中是保留的,这还取决于它是哪个SQL,因此您可能必须小心地在CommandText参数的SQL字符串中直接使用表名


此外,我认为没有必要使用SelectedIndexChanged事件处理程序,除非您需要在用户做出选择后立即执行某些操作。如果您希望用户进行选择,然后单击按钮启动流程,则无需处理更改。

然后他需要遍历数据读取器并在那里输出csv,或者使用它构建其他内容来存储/保存数据。@Justin没错。我想如果这还不够的话,我可以扩展一下。但一旦你有了数据,就应该简单得多。然后你只需循环记录和StringBuilder你的成名之路!对于CSV文件的实际创建,我在这里看到了一些其他帖子,推荐一个免费的开源.NET库,用于导入/导出文本文件。谢谢,我将尝试一下。为了以防万一,添加了更多内容。