473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# and MSAccess

Hi everybody,

I have a question regarding creating a new Access Database programmatically
with C#. Does anyone knows how to? Is there any link out there with good
info?

Thanks for your time
Frank
Nov 16 '05 #1
4 3958
I cant ever recall seeing a full access MDB built from scratch in code -
without using access itself but that doesn't mean you cant do it. Most of
the examples I have come across use an emtpy DB, and then modify the
schema - might be an easier solution.

Regards

John Timney
ASP.NET MVP
"Zagor" <za***@sympatico.ca> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have a question regarding creating a new Access Database programmatically with C#. Does anyone knows how to? Is there any link out there with good
info?

Thanks for your time
Frank

Nov 16 '05 #2
You can do it with ADOX. There are several sources online that explain how to do it. Although, I have not seen any using C#.
Most are VB.NET or VB6. I would think they should be easily adaptable.
You have to add a reference to Microsoft ADO EXT .2.8 for DLL & Security.
Then just add " Imports ADOX

this is a copy of a routine I found and plan to modify for my needs. Maybe it will give you an idea on how it is done.
james

'' Connection to Source database

Dim cnnS As New ADODB.Connection

Catalog of Source database

Dim catS As New ADOX.Catalog, tblS As New ADOX.Table

' Connection to Destination database

Dim cnnD As New ADODB.Connection

accessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + SaveFileDialog1.FileName '

'' Catalog of Destination database

Dim catD As New ADOX.Catalog, tblD As New ADOX.Table

''Connect to Source database

cnnS.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source= " + App.Path + "\Source.mdb;")

catS.ActiveConnection = cnnS

''Connect to Destination database

accessConnection.Open() '"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + mypath() + "\Destination.mdb;")

accessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + openFile.FileName '

catD.ActiveConnection = accessConnection

For lngIndex = 0 To catD.Tables.Count - 1

If catD.Tables(lngIndex).Name = Combo1.Text Then

msgReturn = MsgBox("The table has already existed, do you want to replace it?", vbYesNo, "Replace")

If msgReturn = vbNo Then Exit Sub

catD.Tables.Delete(lngIndex)

Exit For

End If

Next

tblD.Name = Combo1.Text

' Define column to table.

tblD = catD.Tables(Combo1.Text)

With tblD.Columns

For lngIndex = 0 To .Count - 1

tblD.Columns.Append(.Item(lngIndex).Name, _

.Item(lngIndex).Type, _

.Item(lngIndex).DefinedSize)

Next

End With

' Append Table object to Tables collection of Catalog.

catD.Tables.Append(tblD)

catD.Tables.Refresh()

' Copy datas from Source table to Destination table

Dim rstS As New ADODB.Recordset

Dim rstD As New ADODB.Recordset

rstS.CursorLocation = adUseClient

rstS.Open "SELECT * FROM " + Combo1.Text, cnnS, adOpenStatic,

adLockBatchOptimistic()

rstD.CursorLocation = adUseClient

rstD.Open "SELECT * FROM " + Combo1.Text, cnnD, adOpenStatic,

adLockBatchOptimistic()

' Add all data into the destination table

With objDataset1

While Not (.EOF Or .BOF)

objDataset1.AddNew()

For lngIndex = 0 To .Fields.Count - 1

rstD.Fields(.Fields(lngIndex).Name).Value =

Fields(.Fields(lngIndex).Name).Value()

Next

rstD.UpdateBatch(adAffectCurrent)

.MoveNext()

End While

End With

' Release all the objects

If rstS.State = adStateOpen Then rstS.Close()

rstS = Nothing

If rstD.State = adStateOpen Then rstD.Close()

rstD = Nothing

tblS = Nothing

catS = Nothing

If cnnS.State = adStateOpen Then cnnS.Close()

cnnS = Nothing

tblD = Nothing

catD = Nothing

If cnnD.State = adStateOpen Then cnnD.Close()

cnnD = Nothing

Screen.MousePointer = vbDefault


"Zagor" <za***@sympatico.ca> wrote in message news:ud**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have a question regarding creating a new Access Database programmatically with C#. Does anyone knows how to? Is there any
link out there with good info?

Thanks for your time
Frank

Nov 16 '05 #3
I agree, You will have to dig into VB code to find examples of this, and
there are lots of them. Try www.planet-source-code.com, or
www.codeproject.com, there you will find examples of what you are looking
for.
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:O8**************@TK2MSFTNGP15.phx.gbl...
You can do it with ADOX. There are several sources online that explain
how to do it. Although, I have not seen any using C#. Most are VB.NET or
VB6. I would think they should be easily adaptable.
You have to add a reference to Microsoft ADO EXT .2.8 for DLL & Security.
Then just add " Imports ADOX

this is a copy of a routine I found and plan to modify for my needs. Maybe
it will give you an idea on how it is done.
james

'' Connection to Source database

Dim cnnS As New ADODB.Connection

Catalog of Source database

Dim catS As New ADOX.Catalog, tblS As New ADOX.Table

' Connection to Destination database

Dim cnnD As New ADODB.Connection

accessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source= " + SaveFileDialog1.FileName '

'' Catalog of Destination database

Dim catD As New ADOX.Catalog, tblD As New ADOX.Table

''Connect to Source database

cnnS.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source= " + App.Path + "\Source.mdb;")

catS.ActiveConnection = cnnS

''Connect to Destination database

accessConnection.Open() '"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "
+ mypath() + "\Destination.mdb;")

accessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source= " + openFile.FileName '

catD.ActiveConnection = accessConnection

For lngIndex = 0 To catD.Tables.Count - 1

If catD.Tables(lngIndex).Name = Combo1.Text Then

msgReturn = MsgBox("The table has already existed, do you want to replace
it?", vbYesNo, "Replace")

If msgReturn = vbNo Then Exit Sub

catD.Tables.Delete(lngIndex)

Exit For

End If

Next

tblD.Name = Combo1.Text

' Define column to table.

tblD = catD.Tables(Combo1.Text)

With tblD.Columns

For lngIndex = 0 To .Count - 1

tblD.Columns.Append(.Item(lngIndex).Name, _

.Item(lngIndex).Type, _

.Item(lngIndex).DefinedSize)

Next

End With

' Append Table object to Tables collection of Catalog.

catD.Tables.Append(tblD)

catD.Tables.Refresh()

' Copy datas from Source table to Destination table

Dim rstS As New ADODB.Recordset

Dim rstD As New ADODB.Recordset

rstS.CursorLocation = adUseClient

rstS.Open "SELECT * FROM " + Combo1.Text, cnnS, adOpenStatic,

adLockBatchOptimistic()

rstD.CursorLocation = adUseClient

rstD.Open "SELECT * FROM " + Combo1.Text, cnnD, adOpenStatic,

adLockBatchOptimistic()

' Add all data into the destination table

With objDataset1

While Not (.EOF Or .BOF)

objDataset1.AddNew()

For lngIndex = 0 To .Fields.Count - 1

rstD.Fields(.Fields(lngIndex).Name).Value =

Fields(.Fields(lngIndex).Name).Value()

Next

rstD.UpdateBatch(adAffectCurrent)

.MoveNext()

End While

End With

' Release all the objects

If rstS.State = adStateOpen Then rstS.Close()

rstS = Nothing

If rstD.State = adStateOpen Then rstD.Close()

rstD = Nothing

tblS = Nothing

catS = Nothing

If cnnS.State = adStateOpen Then cnnS.Close()

cnnS = Nothing

tblD = Nothing

catD = Nothing

If cnnD.State = adStateOpen Then cnnD.Close()

cnnD = Nothing

Screen.MousePointer = vbDefault


"Zagor" <za***@sympatico.ca> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have a question regarding creating a new Access Database
programmatically with C#. Does anyone knows how to? Is there any link
out there with good info?

Thanks for your time
Frank


Nov 16 '05 #4


Thanks, for the tips guys it helps
"Glenn Wilson" <ir****@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP12.phx.gbl...
I agree, You will have to dig into VB code to find examples of this, and
there are lots of them. Try www.planet-source-code.com, or
www.codeproject.com, there you will find examples of what you are looking
for.
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:O8**************@TK2MSFTNGP15.phx.gbl...
You can do it with ADOX. There are several sources online that explain
how to do it. Although, I have not seen any using C#. Most are VB.NET or
VB6. I would think they should be easily adaptable.
You have to add a reference to Microsoft ADO EXT .2.8 for DLL & Security.
Then just add " Imports ADOX

this is a copy of a routine I found and plan to modify for my needs.
Maybe it will give you an idea on how it is done.
james

'' Connection to Source database

Dim cnnS As New ADODB.Connection

Catalog of Source database

Dim catS As New ADOX.Catalog, tblS As New ADOX.Table

' Connection to Destination database

Dim cnnD As New ADODB.Connection

accessConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " +
SaveFileDialog1.FileName '

'' Catalog of Destination database

Dim catD As New ADOX.Catalog, tblD As New ADOX.Table

''Connect to Source database

cnnS.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source= " + App.Path + "\Source.mdb;")

catS.ActiveConnection = cnnS

''Connect to Destination database

accessConnection.Open() '"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "
+ mypath() + "\Destination.mdb;")

accessConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + openFile.FileName '

catD.ActiveConnection = accessConnection

For lngIndex = 0 To catD.Tables.Count - 1

If catD.Tables(lngIndex).Name = Combo1.Text Then

msgReturn = MsgBox("The table has already existed, do you want to replace
it?", vbYesNo, "Replace")

If msgReturn = vbNo Then Exit Sub

catD.Tables.Delete(lngIndex)

Exit For

End If

Next

tblD.Name = Combo1.Text

' Define column to table.

tblD = catD.Tables(Combo1.Text)

With tblD.Columns

For lngIndex = 0 To .Count - 1

tblD.Columns.Append(.Item(lngIndex).Name, _

.Item(lngIndex).Type, _

.Item(lngIndex).DefinedSize)

Next

End With

' Append Table object to Tables collection of Catalog.

catD.Tables.Append(tblD)

catD.Tables.Refresh()

' Copy datas from Source table to Destination table

Dim rstS As New ADODB.Recordset

Dim rstD As New ADODB.Recordset

rstS.CursorLocation = adUseClient

rstS.Open "SELECT * FROM " + Combo1.Text, cnnS, adOpenStatic,

adLockBatchOptimistic()

rstD.CursorLocation = adUseClient

rstD.Open "SELECT * FROM " + Combo1.Text, cnnD, adOpenStatic,

adLockBatchOptimistic()

' Add all data into the destination table

With objDataset1

While Not (.EOF Or .BOF)

objDataset1.AddNew()

For lngIndex = 0 To .Fields.Count - 1

rstD.Fields(.Fields(lngIndex).Name).Value =

Fields(.Fields(lngIndex).Name).Value()

Next

rstD.UpdateBatch(adAffectCurrent)

.MoveNext()

End While

End With

' Release all the objects

If rstS.State = adStateOpen Then rstS.Close()

rstS = Nothing

If rstD.State = adStateOpen Then rstD.Close()

rstD = Nothing

tblS = Nothing

catS = Nothing

If cnnS.State = adStateOpen Then cnnS.Close()

cnnS = Nothing

tblD = Nothing

catD = Nothing

If cnnD.State = adStateOpen Then cnnD.Close()

cnnD = Nothing

Screen.MousePointer = vbDefault


"Zagor" <za***@sympatico.ca> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have a question regarding creating a new Access Database
programmatically with C#. Does anyone knows how to? Is there any link
out there with good info?

Thanks for your time
Frank



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brad Burke | last post by:
From a windows .Net app, I need to 1) check if MSAccess is already open to a certain Access application. If so, then open a form and find a certain record. 2) If not open, then open MSAccess and...
15
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
0
by: Richard Beacroft | last post by:
Trying to write a C# Windows App to export all objects and content from 2 MSAccess 97 databases for comparison analysis. very little documentation found. Have managed to instantiate MSAccess,...
4
by: Annick Van Hoof | last post by:
Hi, I'm having my ASP.NET application hosted at a Windows 2003 server (IIS6). This works almost all the time (I have uptimes of 100% on most days), but then all of a sudden a few days in a row I...
0
by: MLH | last post by:
I've been unable to determine the correct setting to force a copy of msaccess.exe to be written to target disk during runtime installation process. It may be that the runtime installation...
2
by: reidarT | last post by:
I am making an installation program for an Access application. The application is run on both Office 2000, 2002, 2003 and soon 2007 versions of Office, When I install the application I need a...
1
by: rolandsk | last post by:
I need help to schedule a MsAccess procedure I want the procedure to run in backgrund and not the MsAccess file to be open during the time when the procedure runs. The procedure in MsAccess picks...
1
by: vikramrawal | last post by:
hi, I am using C# with MSAccess XP. I need to insert bulk of records around 10000 in MSAccess. So obviously if I inserts each record individually then it will take lot of time. By using...
0
nehashri
by: nehashri | last post by:
hi, I need one help regarding Asp with MsAccess database. Problem. ======== i want to retrive stored image from the MSAccess database using asp.
0
by: bbrewder | last post by:
I am struggling with some MSAccess automation issues. Basically, we have a .Net application that uses MSAccess for reporting (legacy code). We are able to launch MSAccess fine and even work with...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.