开发者

In C#, How should i close running threads ( Access DB )?

开发者 https://www.devze.com 2023-03-20 20:00 出处:网络
VS 2008 / C# / MS Access 2003 / 2007 I have attached Code Snippet which throws exception on Release Mode. In Debug Mode, Application works great.

VS 2008 / C# / MS Access 2003 / 2007

I have attached Code Snippet which throws exception on Release Mode. In Debug Mode, Application works great.

In order to test this piece of code, I moved to Console Application. When ever I run the application, it shows MS Access DB is already in Use.

So, before executing this piece of code, I need to Kill MS Access DB Process. Whether it is in use or not, I need to kill all the buffer process.

How should I kill the process before I execute this piece of code ?

try
    {

        Access.Application access1 = new Access.Application();

        // Open the Access database for exclusive access
        string sSalisburyAccessDB = Server.MapPath("~/App_Data/Salisbury.mdb");

        access1.OpenCurrentDatabase(sSalisburyAccessDB, true, null);

        // Drop the existing table data

        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "drug");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "patplan");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "plans");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "price");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "rx");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "patient");
        access1.DoCmd.DeleteObject(Access.AcObjectType.acTable, "plntrak");

        // Run the saved import
        access1.DoCmd.RunSavedImportExport("SalisburyODBC");

        // Close the database
        access1.CloseCurrentDatabase();

        // Quit MS Access
        access1.Quit(Access.AcQuitOption.acQuitSaveAll);

       Response.Write("s开发者_如何学编程uccessful");

    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }


I suspect the issue is you dont close the connection if an exception is raised in the main block. Try putting the access1.CloseCurrentDatabase(); in a finally block.

EDIT If you really want to kill the process then you can use Process.Kill. I would recomend against it though as you could cause your users to lose data.

0

精彩评论

暂无评论...
验证码 换一张
取 消