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

C# Insert with two MS Access databases

30
I have 2 MS Access 2003 databses, one is third party so I cannot modify it, and the other is a db I designed to grab the info I need and add a few other columns. I am writing a C# program to perform an Insert of all the records that exist in the 3rd party db, but not in my DB, but I cannot figure out how to do this.

I originally wrote the code in Access VBA via a form timer, a linked table to the third party db (CZPrintJobs) into my db, and the below SQL statement. this worked, but I was told that C# 2003 would be the better way to handle this. I'd rather not link the table as it's twice the space and mean location is less flexible.

I realize the oleDBcommand only contains 1 db connection, so I think I have to run this as a select command, and then an insert, but even the select command would require accessing both dbs. Help would be most appreciated!

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO PAPrintJobs ( Id, Copies, Printer, Computer, Owner, Document, SubmittedDate, SubmittedTime, JobStatus, TotalPages, Cost )" +
  2.                 "SELECT CZPrintJobs.Id, " +
  3.                 "CZPrintJobs.Copies, " +
  4.                 "CZPrintJobs.Printer, " +
  5.                 "CZPrintJobs.Computer, " +
  6.                 "CZPrintJobs.Owner, " +
  7.                 "CZPrintJobs.Document, " +
  8.                 "CZPrintJobs.SumittedDate, " +
  9.                 "CZPrintJobs.SumittedTime, " +
  10.                 "CZPrintJobs.JobStatus, " +
  11.                 "CZPrintJobs.TotalPages, " +
  12.                 "CZPrintJobs.Cost " +
  13.                 "FROM CZPrintJobs " +
  14.                 "LEFT JOIN PAPrintJobs " +
  15.                 "ON (CZPrintJobs.Owner = PAPrintJobs.Owner) AND " +
  16.                 "(CZPrintJobs.Id = PAPrintJobs.Id) AND " +
  17.                 "(CZPrintJobs.SumittedDate = PAPrintJobs.SubmittedDate) AND " +
  18.                 "(CZPrintJobs.SumittedTime = PAPrintJobs.SubmittedTime) " +
  19.                 "WHERE (((CZPrintJobs.Id) Like 'O*') AND ((PAPrintJobs.Id) Is Null) AND ((CZPrintJobs.JobStatus) = 'Printed'));";
Sep 6 '07 #1
5 1624
vanc
211 Expert 100+
You can try using two connections to those two databases, because it is not two connections to one database, so it should work fine. I don't see any obstacles here?!! or I may not understand your problem.

cheers.
Sep 7 '07 #2
ewokspy
30
Well here is the code I am using to try and perform the append statement:

Expand|Select|Wrap|Line Numbers
  1.         public void StartDatabaseLoop()
  2.         {
  3.  
  4.             DateTime lastDay = DateTime.Today;
  5.             OleDbConnection dbCZConnection = 
  6.                     new OleDbConnection(configData[0] + this.CZPath);
  7.             OleDbConnection dbPAConnection = 
  8.                     new OleDbConnection(configData[0] + this.PAPath);
  9.  
  10.             this.loopBool = true;
  11.  
  12.             while(loopBool)
  13.             {
  14.  
  15.                 //Open a connection to the DB
  16.                 dbCZConnection.Open();    
  17.                 dbPAConnection.Open();    
  18.  
  19.                 OleDbDataAdapter myAdapter = new OleDbDataAdapter();    
  20.  
  21.                 //Create the SQL Command
  22.                 OleDbCommand myAppend = new OleDbCommand(
  23.                         BuildSQLAppendString(), dbPAConnection);
  24.  
  25.                 int iRows = myAppend.ExecuteNonQuery();
  26.  
  27.                 dbCZConnection.Close();
  28.                 dbPAConnection.Close();
  29.  
  30.                 //Wait append time or for interruption
  31.                 ThreadController.WaitOne(1000*this.AppendInterval,false);
  32.  
  33.             }//end while
  34.  
  35.  
  36.         }
Note at Line 22 I am calling the dbCommand, but it will only allow me to supply one connection, not two. Even with the tables in the same database and using the supplied query the query doesn't seem to go through. In essence I am asking how you do a select statement that accesses two difference tables in two different Access databases, since once I can get that select statement I can then append the results in a seperate insert command.
Sep 7 '07 #3
vanc
211 Expert 100+
Well here is the code I am using to try and perform the append statement:

Expand|Select|Wrap|Line Numbers
  1.         public void StartDatabaseLoop()
  2.         {
  3.  
  4.             DateTime lastDay = DateTime.Today;
  5.             OleDbConnection dbCZConnection = 
  6.                     new OleDbConnection(configData[0] + this.CZPath);
  7.             OleDbConnection dbPAConnection = 
  8.                     new OleDbConnection(configData[0] + this.PAPath);
  9.  
  10.             this.loopBool = true;
  11.  
  12.             while(loopBool)
  13.             {
  14.  
  15.                 //Open a connection to the DB
  16.                 dbCZConnection.Open();    
  17.                 dbPAConnection.Open();    
  18.  
  19.                 OleDbDataAdapter myAdapter = new OleDbDataAdapter();    
  20.  
  21.                 //Create the SQL Command
  22.                 OleDbCommand myAppend = new OleDbCommand(
  23.                         BuildSQLAppendString(), dbPAConnection);
  24.  
  25.                 int iRows = myAppend.ExecuteNonQuery();
  26.  
  27.                 dbCZConnection.Close();
  28.                 dbPAConnection.Close();
  29.  
  30.                 //Wait append time or for interruption
  31.                 ThreadController.WaitOne(1000*this.AppendInterval,false);
  32.  
  33.             }//end while
  34.  
  35.  
  36.         }
Note at Line 22 I am calling the dbCommand, but it will only allow me to supply one connection, not two. Even with the tables in the same database and using the supplied query the query doesn't seem to go through. In essence I am asking how you do a select statement that accesses two difference tables in two different Access databases, since once I can get that select statement I can then append the results in a seperate insert command.
Sorry about this late, I'm too busy with the current project, can't remember anything. In this case and I understand your idea. I never hear about get data in two database with only one query statement, it sounds great but I don't think we can do that.
To do this we make another way, make to select queries, then manually merge the results to get the goal you want. If you understand your query, of course you do, and what it does behind the scene, then you can easily manipulate your data.
Two connections, two commands, two data readers, ...... one result :).

cheers.
Sep 13 '07 #4
ewokspy
30
Thank you. That pretty much confirms it with other sources I've asked. I do understand that the response is to merge the databases into one, but sometimes that just isn't possible...I wonder when the functionality to do this type of query will get implemented...

OH well. thanks.
Sep 17 '07 #5
vanc
211 Expert 100+
Thank you. That pretty much confirms it with other sources I've asked. I do understand that the response is to merge the databases into one, but sometimes that just isn't possible...I wonder when the functionality to do this type of query will get implemented...

OH well. thanks.
Yes, who knows!!!

cheers.
Sep 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: thejackofall | last post by:
Hi. This is killing me. I am trying to insert a row into an Access table, but it's giving me an exception as below. "Syntax error in INSERT INTO statement." When I debug and look at the...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
5
by: Klemens | last post by:
I get SQL30090 reason 18 by trying to do an insert in a federated table and an update in a local table in one transaction Do I have to change some settings to get done or ist this not possible by...
6
by: Larry Johnson | last post by:
I have two similar SQL Server databases each with a table named Payments. PaymentID is an identity field and the primary, unique, key in both tables. There is one other key but it is not unique. ...
2
by: E. Paul Wileyto | last post by:
Can anyone help. I would like to create a table that contains references to a series of JPG files, so that I can view each image and related data in a form, page by page. It seems that the only...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
0
by: RP_DEV | last post by:
I have 3000+ MS Access databases that I need to develop a solution/script for to import into an Oracle 10 staging database. Using Oracle ODBC drivers, I've attemped to test the inserts using a...
21
by: Killer42 | last post by:
Hi all. I’m almost embarrassed to ask this one, but in fact most of my dabbling in Access has been at a fairly simple level, via the GUI. Now I need to do something slightly deeper, and don’t know...
1
by: creamy | last post by:
Hi Guys, There is the access DB, and the SQL server. I'm building an application that can insert a new person, and details, into a DB. (simple stuff?!) i'm however using a typed dataset created in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.