close
在網路上找到一篇處理ACCESS的文章(http://www.webjx.com/htmldata/2007-10-18/1192714708.html),
說明:要處理ACCESS檔案,必須先引用兩個DLL (加入參考):
1.用來新建ACCESS 。C:\Program Files\Common Files\System\ado\msadox.dll (ADOX命名空間)
2.用來壓縮ACCESS 。C:\Program Files\Common Files\System\ado\msjro.dll (JRO命名空間)
01
//新建ACCESS(DBPath=檔案路徑)
02
public static void CreateAccess(string DBPath)
03
{
04
if (File.Exists(DBPath))//檢查數據庫是否已存在
05
{
06
throw new Exception("檔案不存在!");
07
}
08
DBPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath;
09
//創建一個CatalogClass對象實例
10
ADOX.CatalogClass cat = new ADOX.CatalogClass();
11
//使用CatalogClass對象的Create方法創建ACCESS數據庫
12
cat.Create(DBPath);
13
}
14
15 //壓縮ACCESS(DBPath=檔案路徑)
16
public static void CompactAccess(string DBPath)
17
{
18
if (File.Exists(DBPath))//檢查數據庫是否已存在
19
{
20
throw new Exception("檔案不存在!");
21
}
22
string temp = DateTime.Now.ToShortDateString() + ".bak";
23
temp = DBPath.Substring(0, DBPath.LastIndexOf("\\") + 1) + temp;
24
//定義臨時數據庫的連接字符串
25
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
26
//定義目標數據庫的連接字符串
27
string DBPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath;
28
//創建一個JetEngineClass對象的實例
29
JRO.JetEngineClass jt = new JRO.JetEngineClass();
30
//使用JetEngineClass對象的CompactDatabase方法壓縮修複數據庫
31
jt.CompactDatabase(DBPath2, temp2);
32
//拷貝臨時數據庫到目標數據庫(覆蓋)
33
File.Copy(temp, DBPath, true);
34
//最後刪除臨時數據庫
35
File.Delete(temp);
36
}
37
38
//備份ACCESS(oldDBPath=要備份的檔案路徑;newDBPath=備份檔儲存路徑)
39
public void BackUpDB(string oldDBPath, string newDBPath)
40
{
41
if (!File.Exists(oldDBPath))
42
{
43
throw new Exception("檔案不存在!");
44
}
45
try
46
{
47
File.Copy(oldDBPath, newDBPath, true);
48
}
49
catch (IOException ixp)
50
{
51
throw new Exception(ixp.ToString());
52
}
53
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15 //壓縮ACCESS(DBPath=檔案路徑)
16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

全站熱搜