|
本帖最后由 lipton 于 2012-11-20 11:48 编辑
题目来源与VBA版 http://club.excelhome.net/forum. ... 087&page=2#lastpost
原想用VBA解决,那个 msjro.dll在64位win7不让注册。正好在学习C# ,尝试实际应用。
代码如下:- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.OleDb;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Microsoft.Office.Interop.Access.Dao;
- namespace AccessFixDemo
- {
- public partial class Form1 : Form
- {
- public string strS;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- openFileDialog1.InitialDirectory = "D://Patch";
- openFileDialog1.Filter = "All files (*.*)|*.accdb";
- openFileDialog1.FilterIndex = 2;
- openFileDialog1.RestoreDirectory = true;
- string strT = "d:\\tempAccess.accdb";
- DBEngine db = new Microsoft.Office.Interop.Access.Dao.DBEngine();
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- strS = openFileDialog1.FileName.ToString();
- if (this.textBox1.Text.Length > 0)
- {
- //有密码
- try
- {
- db.CompactDatabase(strS, strT, null, null, ";pwd=" + this.textBox1.Text);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- else
- {
- //无密码
- try
- {
- db.CompactDatabase(strS, strT);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
-
- }
- System.IO.File.Delete(strS);
- System.IO.File.Move(strT, strS);
- MessageBox.Show("压缩完成");
-
- }
- }
-
- }
- }
复制代码 |
|