Access

来源:互联网 发布:企业大数据应用平台 编辑:程序博客网 时间:2024/04/29 18:32

using System;
using System.IO;
using System.Windows.Forms;
using Access = Microsoft.Office.Interop.Access;
// 添加引用->.NET-> dao,Microsoft.Office.Interop.Access

namespace WinFormAccess
{
    public partial class FormAccess : Form
    {
        public FormAccess()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                string dbPath = Path.Combine(Application.StartupPath, "Images.mdb");
                if (File.Exists(dbPath))
                    File.Delete(dbPath);
                Access.Application newAccess = new Access.Application();
                newAccess.NewCurrentDatabase(dbPath); // 创建数据库。
                dao.Database db = newAccess.CurrentDb();
                db.Execute("create table [Images] (Name string primary key, Bytes LongBinary)", Type.Missing);
                db.NewPassword("", "jinzhexian"); // 设置密码。
                db.Close();
                newAccess.Quit(Access.AcQuitOption.acQuitSaveNone);
                newAccess = null;
                GC.Collect();
                System.Diagnostics.Process.Start(dbPath);
            }
            catch (Exception se)
            {
                MessageBox.Show(se.Message);
            }
        }
    }
}