如何将数据从Access导出到文本文件 [英] How to export data from Access to a text file

查看:157
本文介绍了如何将数据从Access导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

净额.我的问题是如何使用vb.net将数据从Access导出到文本文件.在我的数据库中,具有由FirstName和LastName组成的Table1,因此我希望将此数据导出到文本文件.

net. My question is how to export data from Access to a text file using vb.net. In my database has Table1 which consists FirstName and LastName so I want this data to be exported to a text file.

我偶然发现了这段代码并运行了它,并在编译时将其导出,但是没有任何内容导出到文本文件中.有人可以帮我吗?

I stumble on this code and run it and when compiled it but nothing is export to the text file. Can Someone help my with this?

    Dim connetionString As String
    Dim cnn As OleDbConnection
    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Scripts\db.mdb;"
    cnn = New OleDbConnection(connetionString)

        cnn.Open()
        Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Text;HDR=No;DATABASE=C:\Scripts\TextFiles].[Result.txt] FROM Table1", cnn)
        cnn.Close()

提前谢谢!

推荐答案

之所以提出这个问题,是因为您实际上可以从网站上找到很多示例.

This question is downvoted is because you can actually find plenty of example from the website.

也许您不知道要搜索哪个关键字? 尝试类似数据集到文本框"的方法吗? 还是这里的东西? 将C#数据集导出到文本文件 已更新 我了解您可能是vb的新手.我不会给您确切的代码,但会告诉您您可以做什么.

Perhaps you do not know what keyword to search? Try something like "dataset to textbox"? or something here? Export a C# DataSet to a text file Updated I understand you may be new to vb. I will not give you the exact code but tell you what you can do.

首先,声明一个dataTable/dataset(我希望使用DataTable)来保存来自DB的查询结果.

First, declare a dataTable/dataset (I would prefer DataTable) to hold your query result from DB.

    Dim dtresult As DataTable = 'Result from DB

然后循环遍历数据表行,并将数据追加到字符串构建器中(或您希望构建字符串的任何其他方式) 然后将字符串附加到txt文件中. 这是您可以做的.

Then loop through the datatable rows and get the data append into a string builder(or any other way you like to build your string) Then append the string into the txt file. This is something you can do.

更新2 好吧,像这样.

    Private Sub DataTableToTXT()

    Dim connetionString As String
    Dim cnn As OleDbConnection
    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Scripts\db.mdb;"
    cnn = New OleDbConnection(connetionString)

    Dim dtResult As New DataTable
    cnn.Open()
    'Change the query
    Dim dataAdap As New OleDbDataAdapter("SELECT * FROM TABLE1", cnn)
    dataAdap.Fill(dtResult)
    cnn.Close()

    'Change the path to your desired path
    Dim exportPath As String = "C:\Export\"
    Dim exportFileName As String = "data.txt"

    If Not Directory.Exists(exportPath) Then
        Directory.CreateDirectory(exportPath)
    End If

    Dim writer As New StreamWriter(exportPath + exportFileName)
    Try
        Dim sb As New StringBuilder

        For Each row As DataRow In dtResult.Rows
            sb = New StringBuilder
            For Each col As DataColumn In dtResult.Columns
                sb.Append(row(col.ColumnName))
            Next
            writer.WriteLine(sb.ToString())
        Next
    Catch ex As Exception
        Throw ex
    Finally
        If Not writer Is Nothing Then writer.Close()
    End Try

End Sub

这篇关于如何将数据从Access导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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