Quit() 使 Access 暂时可见 [英] Quit() causes Access to become visible for a moment

查看:64
本文介绍了Quit() 使 Access 暂时可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到访问数据库并检索信息.

I'm trying to connect to an access DB and retrieve information.

一切进展顺利,项目几乎完成,但我需要弄清楚如何退出互操作.

Everything is going well the project is almost complete but I need to figure out how to quit interop.

我首先创建我的访问应用程序,然后确保将可见设置为 false.(我没有看到 Access)但是一旦我的代码到达 app.Quit() 行(尽管使用 step 验证)Access 在屏幕上闪烁,只是为了再次消失.

I start by creating my access application then I make sure visible is set to false. (I don't see Access) but as soon as my code get to line app.Quit() (verified using step though) Access flashes on the screen just to disappear again.

有关附加信息:如果我通过 Access 窗口执行步骤不会消失,并且我无法手动关闭它(红色 x 右上角).该应用程序只是重新打开空白.我必须强行关闭它.

For added information: If I do a step through the Access window doesn't disappear and I can't manual close it (red x top right). The application just reopens blank. I have to force close it.

正如您在我注释掉的代码中看到的,我使用 Process Kill 来确保屏幕上没有任何闪烁,但这会导致我的应用程序变得不稳定(由于崩溃而创建了许多访问数据库备份).

As you can see in my commented out code I use a Process Kill to make sure that nothing flashes on the screen but that causes my application to become unstable (so many access DB backups created due to crash).

如果您遇到正常情况(空白凝视).如果您有直觉,请至少让我知道从哪里开始挖掘.

If you are experiences the normal (blank stare). Please at least let me know where to start digging if you have a gut feeling.

static public DataTable ExecuteSQLToDataTable(string sql)
        {
            DataTable dt = new DataTable();

            lock (Locks.AccessDB)
            {
                Microsoft.Office.Interop.Access.Application accApp = new Microsoft.Office.Interop.Access.Application();
                accApp.Visible = false;
                Microsoft.Office.Interop.Access.Dao.Recordset rst = null;
                Microsoft.Office.Interop.Access.Dao.Database cdb = null;


                try
                {
                    accApp.OpenCurrentDatabase(ConnectionDatabase.DatabasePath, false, @"[somepassword]");

                    cdb = accApp.CurrentDb();
                    rst =
                        cdb.OpenRecordset(sql,
                        Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);
                    rst.MoveLast();
                    int recordCount = rst.RecordCount;
                    rst.MoveFirst();
                    object[,] recordsArray = (object[,])rst.GetRows(recordCount);

                    var rowCount = recordsArray.GetLength(1);
                    var columnCount = recordsArray.GetLength(0);

                    var dtTemp = new DataTable();
                    foreach (Microsoft.Office.Interop.Access.Dao.Field fld in rst.Fields)
                        dt.Columns.Add(fld.Name, typeof(string));

                    foreach (var r in Enumerable.Range(1, rowCount))
                        dt.Rows.Add(Enumerable.Range(1, columnCount)
                            .Select(c => recordsArray[c - 1, r - 1]).ToArray());

                }
                catch
                {
                    //TODO Add catch
                }
                finally
                {

                    GetWindowThreadProcessId(accApp.hWndAccessApp(), out int id);
                    if (rst != null) { Marshal.ReleaseComObject(rst); }
                    if (cdb != null) { Marshal.ReleaseComObject(cdb); }
                    if (accApp != null) { accApp.Quit(); Marshal.ReleaseComObject(accApp); }
                    rst = null;
                    cdb = null;
                    accApp = null;
                    //Process.GetProcessById(id).Kill();

                }

                return dt;
            }
        }

Locks 是一个静态类Locks.AccessDB 只是一个空对象

Locks is a static class Locks.AccessDB is just an empty object

推荐答案

我倾向于相信 Access 正在做一些自己的事情,以便在您调用所有 Quit() 时在屏幕上显示自己.

I am inclined to believe that Access is doing something of its own to show itself on screen when you call all Quit().

您可以通过使用 SetWindowPos 将 Access 窗口移出屏幕来解决此问题,这样您在调用 Quit() 时就不会看到它.

You can workaround the issue by using SetWindowPos to move the Access window off the screen so you never see it when you call Quit().

这篇关于Quit() 使 Access 暂时可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆