ImageToAccess/Program.cs

本文介绍了一个使用C#语言创建并与Microsoft Access数据库交互的应用程序示例。该应用通过Mutex确保仅一个实例运行,并演示了如何创建数据库文件、定义表结构及设置窗口权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using Access = Microsoft.Office.Interop.Access;

namespace ImageAccess
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/ImageAccess", out isCreated))
            {
                if (isCreated)
                {
                    string dbPath = Path.Combine(Application.StartupPath, "Images.mdb");
                    if (File.Exists(dbPath))
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        using (RegistryKey subKey = Application.UserAppDataRegistry)
                        {
                            FormImageAccess frame = new FormImageAccess();
                            subKey.SetValue("Handle", frame.Handle);
                            Application.Run(frame);
                        }
                        newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                    }
                    else
                    {
                        Access.Application newAccess = new Access.Application();
                        newAccess.NewCurrentDatabase(dbPath);
                        dao.Database db = newAccess.CurrentDb();
                        db.Execute("create table [Images] (Name Text primary key, Bytes Image)", Type.Missing);
                        db.NewPassword("", "jinzhexian");
                        db.Close();
                        newAccess.Quit(Access.AcQuitOption.acQuitSaveNone);
                        newAccess = null;
                        GC.Collect();
                        Application.ExitThread();
                    }
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        IntPtr handle = new IntPtr(Convert.ToInt32(subKey.GetValue("Handle")));
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                    Application.ExitThread();
                }
            }
            #endregion
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值