473,385 Members | 1,292 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,385 software developers and data experts.

Copying from one Database to another VB 2005

I have 2 Access '97 databases. I am trying to migrate all the data from one
to the other (append to existing records). I do not have Access '97 and
opening with Access XP or later causes problems. I have found I can connect
to the databases in Visual Studio 2005 and insert records, etc, so I'm
trying to write my code using VB 2005.

I have 2 connections, 2 datasets... OldDS and NewDS. First I populate both
datasets:
OldDS.TableAdapter1.Fill(OldDS.Table1)
NewDS.TableAdapter1.Fill(NewDS.Table1)
This works fine, I've queried the datasets, they are both populated with
data from the tables, ready to go.

Here's where I have the problem, transferring data from the old dataset into
the new one. I've tried numerous approaches, here is the most recent
attempt:
For i As Integer = 0 To oldDS.Table1.Count - 1

' Copy Row from oldDataSet, INSERT into newDataSet

Dim r As DataRow = oldDS.Table1.Rows(i)

newDS.Table1.AddTable1Row(r.Item(0), r.Item(1), r.Item(2), r.Item(3),
r.Item(4))

Me.ProgressBar1.Increment(1)

Next

What happens is I get constraint errors.. type "DBNull" cannot be converted
to <insert type here>. I have checked both Datasets and constraints are
OFF. Why would it do this? All fields should allow Null values, but any
Nulls in the Old table are causing all sorts of problems. I've set the
default for Strings to (Empty) using the dataset designer and this seems to
help those fields but any other field type (DateTime for example) will throw
an exception.

Thanks,
Ryan
Oct 25 '06 #1
6 4534
Hi Ryan,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to move data from one Access
database to the other. If there is any misunderstanding, please feel free
to let me know.

I would like to know if the schema of OldDS is the same as NewDS's. If so,
I don't think you need to copy the rows from one dataset to another. When
data is filled to OldDS, you can call SetAdded on each line to set the
RowState to Added. Here is an example:

For Each dr As DataRow In OldDS.Tables(0).Rows
dr.SetAdded();
Next

Then you can use a TableAdapter or DataAdapter to update the "Added" data
to the New Access database. HTH.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Oct 26 '06 #2
"Kevin Yu [MSFT]" <v-****@online.microsoft.comwrote in message
news:IJ****************@TK2MSFTNGXA01.phx.gbl...
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to move data from one Access
database to the other. If there is any misunderstanding, please feel free
to let me know.
This is correct. These are 2 Access 97 databases. One other thing I've
noticed is these tables don't even use Primary Keys. They are laid out more
like an Excel spreadsheet without any regard to rules of normalcy (just
FYI - don't think this will affect anything). These tables are used by 3rd
party software so I cannot alter the table layouts without risking losing
compatibility with the software that uses them.
I would like to know if the schema of OldDS is the same as NewDS's. If so,
I don't think you need to copy the rows from one dataset to another. When
data is filled to OldDS, you can call SetAdded on each line to set the
RowState to Added. Here is an example:
The schemas are the same, the only difference is the connection info (one
for each physical database).
For Each dr As DataRow In OldDS.Tables(0).Rows
dr.SetAdded();
Next

Then you can use a TableAdapter or DataAdapter to update the "Added" data
to the New Access database. HTH.
Ok, I've done this, but I'm unsure about what I'm suppose to do with the
TableAdapter to "update the Added data". How is setting the table state in
the oldDS for each row to "Added" going to help me move that data to the new
database?

Thanks,
Ryan
Oct 26 '06 #3
One other thing I've noticed is these tables don't even use Primary Keys.
They are laid out more like an Excel spreadsheet without any regard to
rules of normalcy (just FYI - don't think this will affect anything).
Forgot to mention.. this does affect something. When I create my DataSets -
Update commands are not generated by the designer. I believe this is due to
the tables not having a primary key. Therefore, although I have been
successful copying data from one dataset to another, I have not been
successful using "Update" to commit the data back to the database
(newTA.Update(newDS.table(0))). Hence why I have been trying to use an
Insert command for each row to add the data directly to the database.
Oct 26 '06 #4
On Wed, 25 Oct 2006 08:57:51 -0500, "Ryan" <Ty****@newsgroups.nospamwrote:

¤ I have 2 Access '97 databases. I am trying to migrate all the data from one
¤ to the other (append to existing records). I do not have Access '97 and
¤ opening with Access XP or later causes problems. I have found I can connect
¤ to the databases in Visual Studio 2005 and insert records, etc, so I'm
¤ trying to write my code using VB 2005.
¤
¤ I have 2 connections, 2 datasets... OldDS and NewDS. First I populate both
¤ datasets:
¤ OldDS.TableAdapter1.Fill(OldDS.Table1)
¤ NewDS.TableAdapter1.Fill(NewDS.Table1)
¤ This works fine, I've queried the datasets, they are both populated with
¤ data from the tables, ready to go.
¤
¤ Here's where I have the problem, transferring data from the old dataset into
¤ the new one. I've tried numerous approaches, here is the most recent
¤ attempt:
¤ For i As Integer = 0 To oldDS.Table1.Count - 1
¤
¤ ' Copy Row from oldDataSet, INSERT into newDataSet
¤
¤ Dim r As DataRow = oldDS.Table1.Rows(i)
¤
¤ newDS.Table1.AddTable1Row(r.Item(0), r.Item(1), r.Item(2), r.Item(3),
¤ r.Item(4))
¤
¤ Me.ProgressBar1.Increment(1)
¤
¤ Next
¤
¤ What happens is I get constraint errors.. type "DBNull" cannot be converted
¤ to <insert type here>. I have checked both Datasets and constraints are
¤ OFF. Why would it do this? All fields should allow Null values, but any
¤ Nulls in the Old table are causing all sorts of problems. I've set the
¤ default for Strings to (Empty) using the dataset designer and this seems to
¤ help those fields but any other field type (DateTime for example) will throw
¤ an exception.

Why not just use a SQL statement?

Open a connection to the destination database and specify the source database path in your SQL
statement:

Dim AccessConn As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data Source=C:\Test
Files\destination.mdb")

AccessConn.Open()

AccessCommand.CommandText = "INSERT INTO [Data] SELECT * FROM [MS Access;DATABASE=C:\Test
Files\source.mdb;].[Data]"
AccessCommand.ExecuteNonQuery()
AccessConn.Close()
Paul
~~~~
Microsoft MVP (Visual Basic)
Oct 26 '06 #5
Hi Ryan,

Paul's advice is very good. It might be the simplest way here to use on a
SQL statement.

As you can see, yes, the update commands are not generated because there is
no primary key in the table. So, you have to add a primary key to the
destination table in the Access database.

In this case, we only need one instance of the DataSet here since the two
schemas are identical. You can create 2 OleDbConnections which represents
connection to the 2 databases. A TableAdapter has a property named
Connection. Here are the steps:

1. Assign the first connection to TableAdapter.connection and get data from
source database.
2. Use the method I mentioned in my last post to set all the RowState to
Added.
3. Assign the second connection to TableAdapter.connection and call Update
to put data to destination database.

HTH.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Oct 27 '06 #6
Few code tweaks and this worked great. Couldn't get anything else to work
(working with Datasets). Thanks both of you for the help.

"Paul Clement" <Us***********************@swspectrum.comwrote in message
news:nf********************************@4ax.com...
On Wed, 25 Oct 2006 08:57:51 -0500, "Ryan" <Ty****@newsgroups.nospam>
wrote:

¤ I have 2 Access '97 databases. I am trying to migrate all the data from
one
¤ to the other (append to existing records). I do not have Access '97 and
¤ opening with Access XP or later causes problems. I have found I can
connect
¤ to the databases in Visual Studio 2005 and insert records, etc, so I'm
¤ trying to write my code using VB 2005.
¤
¤ I have 2 connections, 2 datasets... OldDS and NewDS. First I populate
both
¤ datasets:
¤ OldDS.TableAdapter1.Fill(OldDS.Table1)
¤ NewDS.TableAdapter1.Fill(NewDS.Table1)
¤ This works fine, I've queried the datasets, they are both populated with
¤ data from the tables, ready to go.
¤
¤ Here's where I have the problem, transferring data from the old dataset
into
¤ the new one. I've tried numerous approaches, here is the most recent
¤ attempt:
¤ For i As Integer = 0 To oldDS.Table1.Count - 1
¤
¤ ' Copy Row from oldDataSet, INSERT into newDataSet
¤
¤ Dim r As DataRow = oldDS.Table1.Rows(i)
¤
¤ newDS.Table1.AddTable1Row(r.Item(0), r.Item(1), r.Item(2), r.Item(3),
¤ r.Item(4))
¤
¤ Me.ProgressBar1.Increment(1)
¤
¤ Next
¤
¤ What happens is I get constraint errors.. type "DBNull" cannot be
converted
¤ to <insert type here>. I have checked both Datasets and constraints are
¤ OFF. Why would it do this? All fields should allow Null values, but
any
¤ Nulls in the Old table are causing all sorts of problems. I've set the
¤ default for Strings to (Empty) using the dataset designer and this seems
to
¤ help those fields but any other field type (DateTime for example) will
throw
¤ an exception.

Why not just use a SQL statement?

Open a connection to the destination database and specify the source
database path in your SQL
statement:

Dim AccessConn As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;Data
Source=C:\Test
Files\destination.mdb")

AccessConn.Open()

AccessCommand.CommandText = "INSERT INTO [Data] SELECT * FROM [MS
Access;DATABASE=C:\Test
Files\source.mdb;].[Data]"
AccessCommand.ExecuteNonQuery()
AccessConn.Close()
Paul
~~~~
Microsoft MVP (Visual Basic)

Oct 27 '06 #7

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

Similar topics

2
by: Don | last post by:
Using JS, how do I copy an associative array from one frame to another? Here's what I tried, and didn't work. In "main" frame referencing "header" frame: var cookies = new Object(); cookies...
5
by: Lyn | last post by:
I am trying to copy selected fields from one table to another, by reading the source table record into a recordset (ADO) and then copying the relevant fields to an SQL statement "INSERT INTO...". ...
16
by: NoodNutt | last post by:
G'day ppl Is there a process whereby I can copy the text labels of all the files in a root directory? I recall going back a long way seeing something about it but did not take any notice as I...
4
by: zMisc | last post by:
Is it possible to copy a table from one schema to another schema by just copying the frm file to the directory for the new schema? What is the best way to create a new database with all the...
1
by: maflatoun | last post by:
Hi, In SQL 2000 if I wanted to take a complete copy of another running sql database all did was create a new database locally and right-click it and select import and point to another database...
3
by: John | last post by:
Hi all, My application updates a sql server 2005 express database prior to copying it with the result being the "in use by another process" and I cannot copy it as a result. I've posted the code...
0
by: berwiki | last post by:
I am trying to copy a table to another SQL 2000 Database, but I continually get errors. When I right-click, choose All-Tasks, Export-Data and go through the DTS settings, I get an 'Unspecified...
5
by: ozzii | last post by:
Hi I am using the following code to copy data from one database table into another database table: SELECT * INTO Products From exportdb.mdb.exporttable However the query simply deletes...
0
by: ElishaThompson | last post by:
I recently had a problem requiring me to locate and copy all records pertaining to a particular client from numerous tables in a database on a given server to tables within an identical database on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.