Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...