Exporting Tables From .mdf file Visual Studio VB.NET

Posted on 2009-02-15
Last Modified: 2013-11-26
Hello experts,

I'm building an application thats linked to a .mdf file and I want to export the data out from one of the tables as a .csv file into a folder called Export Files in the current project path

I have some code I've used in Access and I'm trying to update it into VB.NET in Visual Studio 2008.

Any Ideas on how to change it, I'm having problems.

Dim getTables As String

 Dim csvName As String

 Dim BoatName As String


 'Copies the tblSurveys

 BoatName = Form!lblBoatName.Caption

 getTables = "tblSurveys"

 csvName = CurrentProject.Path & "\" & "Export Files\" & BoatName & getTables & ".csv"


 DoCmd.TransferText transfertype:=acExportDelim, _

   specificationname:="tblSurveys Export Specification", _

   tableName:=getTables, _

   filename:=csvName, _


Open in new window

Question by:davecocks
    LVL 15

    Accepted Solution

    First off, you have to attach the MDF file to a SQL Server, VS 2008 should have come with SQL Express 05 or 08.

    Once you have the database attached, you can connect to it using a SqlCommand.  With the SqlCommand pull the data from the table, and loop through it with a SqlDataReader.

    On each read, write the output to a filestream in .CSV format.  I'm just going to throw together some code.

    Here you go:

    Dim fs As New IO.FileStream("test.csv", IO.FileMode.CreateNew, IO.FileAccess.Write)
    Dim sw As New IO.StreamWriter(fs)
    Dim conn As New Data.SqlClient.SqlConnection("connection string")
    Dim cmd As New Data.SqlClient.SqlCommand("Command Text", conn)
    Dim dr As Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
    While dr.Read()
      sw.WriteLine("{0},{1},{2},{3}", dr(0), dr(1), dr(2), dr(3))
    End While

    Open in new window

    LVL 1

    Author Closing Comment

    Hi Thanks for your help you put me on the right track.

    I found another link with some suggestions if any one else needs some help.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now