Click here to Skip to main content
6,634,665 members and growing! (16,723 online)
Email Password   helpLost your password?
Database » Database » Databases     Intermediate License: The Code Project Open License (CPOL)

Creating and populating MS Access files from other databases, using DTS

By Malik Nasir

An article on creating and populating an automated MS Access file from other databases.
VB, SQL, Windows, .NET, Visual Studio, ADO.NET, DBA, Dev
Posted:31 May 2006
Views:47,313
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.14 Rating: 3.14 out of 5
2 votes, 20.0%
1
3 votes, 30.0%
2

3
2 votes, 20.0%
4
3 votes, 30.0%
5

Sample Image

Introduction

If you are using databases like Oracle or SQL Server and you want to copy data from them to some other database that is not linked to your data in any way, you can do this by using some DTS like SQL Server, but the exact scenario that compelled me to develop this utility was to “do all in an automated way”. I developed this application to convert my source data to MS Access file format but the same can be used to convert any kind of data source to MS Access.

Background

As I mentioned earlier, the reason to develop this application was to do everything in an automated way. That means, I want to periodically convert my source data to MS Access in an automated fashion.

Code

Imports System.IO
Imports System.Data.OleDb
Module Module1
    Sub Main()
        Try
            'Access DB File path
            Dim File_Path As String
            File_Path = _
             AppDomain.CurrentDomain.BaseDirectory & "db.mdb"
            'If Exists already then delete it first
            If File.Exists(File_Path) Then
                File.Delete(File_Path)
            End If
            'Connection String for New DB File
            Dim Access_ConnStr As String = _
              "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
              " Data Source=" & File_Path
            'ADOX.Catalog Object to craete new DB
            Dim Catalog As New ADOX.Catalog()
            Catalog.Create(Access_ConnStr)
            Catalog.ActiveConnection.close()
            Catalog = Nothing
            'OleDb Connection to Create New table
            Dim Access_Conn As OleDb.OleDbConnection
            Access_Conn = New OleDb.OleDbConnection(Access_ConnStr)
            Access_Conn.Open()
            Dim strSQL As String = "CREATE TABLE tblTest (" & _
            " TerritoryID                              Text(20)," & _
            " TerritoryDescription                     Text(50)," & _
            " RegionID                                 Number);"

            Dim cmd As OleDbCommand = Access_Conn.CreateCommand()
            cmd.CommandText = strSQL
            cmd.ExecuteNonQuery()
            'Set and Open Connection of the Source DB
            Dim SrcConnStr As String = "Provider=sqloledb; " & _ 
                "Data Source=(local); Initial Catalog=northwind; " & _ 
                "User Id=sa; Password=;integrated " & _ 
                "security=SSPI;persist security info=False;"
            Dim SrcConn As New OleDbConnection(SrcConnStr)
            SrcConn.Open()
            Dim srcReader As System.Data.OleDb.OleDbDataReader
            strSQL = "select * from Territories"
            Dim SrcCmd As New OleDbCommand(strSQL, SrcConn)
            'Read from Source and populate in the Access DB
            srcReader = SrcCmd.ExecuteReader()
            While srcReader.Read
                strSQL = "insert into tblTest values('" & _
                         srcReader.GetValue(0).ToString & "','" _
                         & srcReader.GetValue(1).ToString & _
                         "','" & srcReader.GetValue(2).ToString & "')"
                cmd.CommandText = strSQL
                cmd.ExecuteNonQuery()
            End While
            cmd.Dispose()
            cmd = Nothing
            Access_Conn.Close()
            Access_Conn.Dispose()
            Access_Conn.ReleaseObjectPool()
            Access_Conn = Nothing
            srcReader.Close()
            srcReader = Nothing
            SrcCmd.Dispose()
            SrcCmd = Nothing
            SrcConn.Close()
            SrcConn.Dispose()
            SrcConn = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

Working with the demo

Download the demo project and unzip it, and then run "DTS.exe". It will generate an MS Access file “db.mdb” in the same directory. If you view this file, it will contain all the data of the Northwind database of SQL Server. Note: To run this demo application, SQL Server must be installed on your machine.

Working with the code

Download the code and open it in Visual Studio .NET, then do the following changes if you want to change this according to your requirement:

  • Change “File_Path” and set it where you want to create a new file.
  • Change the Create Table command at line 26 according to your source table.
  • If you are using databases other than SQL Server then change the connection string for your source DB, at line 36.
  • Change the source ‘Select’ command at line 41.
  • Change the ‘Insert’ command at line 47 according to the source and destination tables.

That’s al...

Points of Interest

  • The application can be designed to import multiple tables.
  • The console application can be scheduled as an automated data import utility.
  • The application can be designed in such a way that the user just has to select a database and click to import the data.

Summary

There are so many tools for DTS, but this is an effort to do the same in my own desired way; and using this single application, many targets can be achieved. So use this in your own way. Happy programming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Malik Nasir


Member
I am currently based in Islamabad,Pakistan. I have done my MS Computer Science and I am involved in Microsoft Visual Studio.net Windows/Web application development since 2003 untill that i have been working in VB, ASP and web scripting languages. Currently i am working in ZonG as Team LEad IT Projects and Planing. I am also the administrator of Business Process
Occupation: Team Leader
Company: ZonG, China Mobile Company
Location: Pakistan Pakistan

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralAdding another Table PinmemberMGDude20:03 11 Jun '08  
Generaldts ? Pinmemberscalpa9811:04 1 Nov '07  
Generalcsv file Pinmemberscalpa989:21 31 Oct '07  
GeneralRe: csv file PinmemberMalik Nasir4:08 1 Nov '07  
Questionupdate DATE datatype into access PinmemberQuestionaire18:01 24 Nov '06  
AnswerRe: update DATE datatype into access PinmemberMalik Nasir20:17 26 Nov '06  
GeneralWhat's the fastest way to populate an MS Access table? PinmemberMelStalcup11:12 28 Jun '06  
GeneralCopying a table from one Access db to another Pinmemberdralexh3:18 9 Jun '06  
GeneralRe: Copying a table from one Access db to another PinmemberJames H0:14 12 Jun '06  
GeneralRe: Copying a table from one Access db to another PinmemberMalik Nasir1:19 12 Jun '06  
GeneralRe: Copying a table from one Access db to another Pinmemberdralexh3:21 12 Jun '06  
GeneralRe: Copying a table from one Access db to another PinmemberMalik Nasir22:41 12 Jun '06  
GeneralDTS? Pinmembermichaelschuer0:34 6 Jun '06  
GeneralRe: DTS? PinmemberMalik Nasir1:41 6 Jun '06  
GeneralRe: DTS? Pinmembervlad.pitaru6:36 6 Jun '06  
GeneralRe: DTS? PinmemberMalik Nasir19:51 8 Jun '06  
GeneralRe: DTS? PinmemberZiener23:48 16 Nov '06  
QuestionHi, Pinmember| Muhammad Waqas Butt |0:06 3 Jun '06  
AnswerRe: Hi, PinmemberMalik Nasir20:34 4 Jun '06  
AnswerRe: Hi, PinmemberJames H0:23 12 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 May 2006
Editor: Chris Maunder
Copyright 2006 by Malik Nasir
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project