C # access, excel, sql and compression access

 1 using System;
 2 using System.Data;
 3 using System.Data.OleDb ;
 4 using System.Linq;
 5 using System.IO;
 6 
 7 OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\123.mdb");
 8 
 9 OleDbCommand comm=new OleDbCommand();
10 
11 comm.Connection=conn;
12 
13 // insert A from B
14 
15 // comm.CommandText=string.Format("INSERT INTO {0} select * from  [{1}].{0} where
16 
17 // TIName='{2}'",tableNames[j],sourceDbPath,tiNames[i]);
18 
19 comm.CommandText="insert into userName select * from [D:\\456.mdb].userName";
20 
21 // insert data from access to excel
22 
23 // comm.CommandText = "select * into [Excel 8.0;database=E:\\123.xls].Sheet1 from [E:\\123.mdb].GlobalParameter";
24 
25 comm.ExecuteNonQuery();
26 
27 comm.Dispose();
28 conn.Close();
29 
30 //Database Compression (internet information)
 31 is  
32  // create Jet engine object 
33 is  Object objJetEngine the Activator.CreateInstance = (Type.GetTypeFromProgID ( " JRO.JetEngine " ));
 34 is  
35  // set parameter array
 36  // The Access you use modified version of the "Jet OLEDB: Engine Type = 5 " in the number.
 37  @ 5 corresponds JET4X format (Access 2000,2002) 
38 is  
39  Object [] = objParams new new  Object [] {
 40 the String.Format ( " Provider = the Microsoft. Jet.OLEDB.4.0; the Data the Source = {0} " , dbPath), // input connection string 
41The String.Format ( " Provider = the Microsoft.Jet.OLEDB.4.0; the Data the Source = {0}; the Jet OLEDB: Engine. 5 the Type = " , tempDbPath) // output connection string 
42  };
 43 is  
44 is  // invoked by reflection CompactDatabase method 
45  the try {
 46 is objJetEngine.GetType () the InvokeMember (. " the CompactDatabase " ,
 47  System.Reflection.BindingFlags.InvokeMethod,
 48  null ,
 49  objJetEngine,
 50  objParams);
 51 is  
52 is } the catch (Exception) {
 53 is Console.WriteLine ( "Database to Compact the Fail: \ R & lt \ n-Can Invoke Compact Command Not! " );
 54 is the Console.ReadKey ( to true );
 55  the throw ;
 56 is  }
 57 is  // delete the original database file 
58  File.Delete (dbPath);
 59  // rename the database file compressed 
60  File.Move (tempDbPath, dbPath);
 61 is  // release Com assembly 
62 is  System.Runtime.InteropServices.Marshal.ReleaseComObject (objJetEngine);
 63 is objJetEngine = null ;

 

Guess you like

Origin www.cnblogs.com/alisa202002/p/12301101.html
Recommended