简体   繁体   中英

Passing data from one database to another database table

I want to take a backup of my Access database Pragmatically. And After taking all data in backup i want to delete data from source database. ( So that it will not take much time while querying and filtering through application.)

The source database name is Data.mdb The destination database name is Backup.mdb Both are protected by same password.

For these purpose i am writing a query in C# like this.

string conString = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=Backup.mdb;Jet    
                   OLEDB:Database Password=12345";
OleDbConnection dbconn = new OleDbConnection();
OleDbDataAdapter dAdapter = new OleDbDataAdapter();
OleDbCommand dbcommand = new OleDbCommand();

try
{
   if (dbconn.State == ConnectionState.Closed)
      dbconn.Open();

 string selQuery = "INSERT INTO [Bill_Master] SELECT * FROM [MS Access;DATABASE="+     
                   "\\Data.mdb" + "; Jet OLEDB:Database Password=12345;].[Bill_Master]";

 dbcommand.CommandText = selQuery;
 dbcommand.CommandType = CommandType.Text;
 dbcommand.Connection = dbconn;
 int result = dbcommand.ExecuteNonQuery();
 }
 catch(Exception ex)   {}

Everything goes fine if i try with without password database file. I think error in passing password on query statement. I am trying to execute through access query but it is saying "Invalid argument". Please is there any other programing logic for doing that. Thanks

prashant YuvaDeveloper

Are Data.mdb and Backup.mdb identically in strcuture? If so, I wouldn't bother copying data via SQL but just copy the whole file.

Try remove the space between the ; and Jet … So the format would be:

INSERT INTO [Bill_Master] SELECT * FROM [MS Access;DATABASE="+     
               "\\Data.mdb" + ";Jet OLEDB:Database Password=12345;].[Bill_Master]

You can copy and rename Data.mdb, and then truncate all the tables in Data.mdb. Far easier than trying to copy a table at a time..

Don't delete data. This becomes a lot mroe difficult in the future to do analysis or inquiries. If it's taking a long time then review indexing or upszing to SQL Server. The Express edition is free and can handle databases up to 4 Gb.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM