Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

vb.net - How to export data from Access to a text file

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:Scriptsdb.mdb;"
    cnn = New OleDbConnection(connetionString)

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

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

    Dim dtresult As DataTable = 'Result from DB

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.

UPDATE 2 Okay, something like this.

    Private Sub DataTableToTXT()

    Dim connetionString As String
    Dim cnn As OleDbConnection
    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Scriptsdb.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...