错误告诉我我还没有关闭连接,但不是吗?

发布于 2024-12-17 17:35:21 字数 2229 浏览 2 评论 0原文

我一定是在这里错过了什么。我正在尝试创建一个表,但收到一条错误消息,告诉我连接仍处于打开状态。但在哪里???我已经重新阅读了代码,但我找不到连接仍然打开的位置...

问题出在这里: objOleDbConnection.Open()

错误说:

You attempted to open a database that is already opened by user 'Admin' on machine 'machine'. Try again when the database is available.

private void sfdNewFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
    // Creating a ADOX object needed to create
    // new MS Access file.
    ADOX.Catalog createMSFile = new ADOX.Catalog();
    // Creating an object for a table.
    Table nTable = new Table();

    // Creating an object allowing me connecting to the database.
    OleDbConnection objOleDbConnection = new OleDbConnection();
    objOleDbConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + sfdNewFile.FileName + ";Persist Security Info=False;Mode=12";
    // Creating command object.
    OleDbCommand objOleDbCommand = new OleDbCommand();
    objOleDbCommand.Connection = objOleDbConnection;

    try
    {
        // Created a new MS Access 2007 file with specified path.
        createMSFile.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
            sfdNewFile.FileName);

        objOleDbConnection.Open();
        objOleDbCommand.CommandText = "CREATE TABLE PersonalData (" +
            "[DataID] AUTOINCREMENT NOT NULL PRIMARY KEY ," +
            "[Type] VARCHAR(40) NOT NULL ," +
            "[URL] VARCHAR(40) NOT NULL ," +
            "[SoftwareName] VARCHAR(40) NOT NULL ," +
            "[SerialCode] VARCHAR(40) NOT NULL ," +
            "[UserName] VARCHAR(40) NOT NULL ," +
            "[Password] VARCHAR(40) NOT NULL";

        objOleDbCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        // Displaying any errors that 
        // might have occured.
        MessageBox.Show("Error: " + ex.Message);
    }
    finally
    {
        // It is importnat to release COM object, in this very order
        // otherwise we eill end up with an error.
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(createMSFile);

        // Closing the connection to the database.
        objOleDbConnection.Close();
    }
}

I must have missed something here. I am trying to create a table but I am getting an error telling me that connection is still open. But where??? I have re-read the code but I can't find where the connection is still open...

Problem lays here: objOleDbConnection.Open()

Error say:

You attempted to open a database that is already opened by user 'Admin' on machine 'machine'. Try again when the database is available.

private void sfdNewFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
    // Creating a ADOX object needed to create
    // new MS Access file.
    ADOX.Catalog createMSFile = new ADOX.Catalog();
    // Creating an object for a table.
    Table nTable = new Table();

    // Creating an object allowing me connecting to the database.
    OleDbConnection objOleDbConnection = new OleDbConnection();
    objOleDbConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + sfdNewFile.FileName + ";Persist Security Info=False;Mode=12";
    // Creating command object.
    OleDbCommand objOleDbCommand = new OleDbCommand();
    objOleDbCommand.Connection = objOleDbConnection;

    try
    {
        // Created a new MS Access 2007 file with specified path.
        createMSFile.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
            sfdNewFile.FileName);

        objOleDbConnection.Open();
        objOleDbCommand.CommandText = "CREATE TABLE PersonalData (" +
            "[DataID] AUTOINCREMENT NOT NULL PRIMARY KEY ," +
            "[Type] VARCHAR(40) NOT NULL ," +
            "[URL] VARCHAR(40) NOT NULL ," +
            "[SoftwareName] VARCHAR(40) NOT NULL ," +
            "[SerialCode] VARCHAR(40) NOT NULL ," +
            "[UserName] VARCHAR(40) NOT NULL ," +
            "[Password] VARCHAR(40) NOT NULL";

        objOleDbCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        // Displaying any errors that 
        // might have occured.
        MessageBox.Show("Error: " + ex.Message);
    }
    finally
    {
        // It is importnat to release COM object, in this very order
        // otherwise we eill end up with an error.
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(createMSFile);

        // Closing the connection to the database.
        objOleDbConnection.Close();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪也成诗 2024-12-24 17:35:21

看来 ADOX 对象也应该关闭它的连接。
请看以下来自 Microsoft 的示例:
http://msdn.microsoft。 com/en-us/library/windows/desktop/ms681562(v=vs.85).aspx

他们的 ADOX 对象名为 cat。
它们具有以下内容:

cat.ActiveConnection = Nothing

这可能会翻译为:

createMSFile.ActiveConnection = null

在您的代码中(可能在第一个 Final 块中)。
我认为是 ADOX 对象造成了差异,在处理它之前,尝试将其 ActiveConnection 设置为 null。

It seems that the ADOX object should close its connection as well.
Look at the following sample from Microsoft:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681562(v=vs.85).aspx

Their ADOX object is named cat.
They have the following:

cat.ActiveConnection = Nothing

Which will probably translate to:

createMSFile.ActiveConnection = null

In your code (probably in the first Finally block).
I think it's the ADOX object that makes the difference, before dispoing it, try to set it's ActiveConnection to null.

耶耶耶 2024-12-24 17:35:21

我必须承认,我认为 ADO 实际上适用于 C++ 和 VB,而不是 .NET 技术。

引用此处

警告 ADO 和 ADO MD 尚未在 Microsoft .NET Framework 环境中经过全面测试。它们可能会导致间歇性问题,尤其是在基于服务的应用程序或多线程应用程序中。本文中讨论的技术只能用作迁移到 ADO.NET 期间的临时措施。您应该仅在进行完整测试以确保不存在兼容性问题后才使用这些技术。不支持以这种方式使用 ADO 或 ADO MD 引起的任何问题。有关详细信息,请参阅 Microsoft 知识库中的以下文章:
840667 在 .NET Framework 应用程序中使用 ADO 和 ADO MD 时收到意外错误

在同一页面上,您将看到以下代码:

using System;
using ADOX;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            ADOX.CatalogClass cat = new ADOX.CatalogClass();

            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
                   "Data Source=D:\\AccessDB\\NewMDB.mdb;" +
                   "Jet OLEDB:Engine Type=5");

            Console.WriteLine("Database Created Successfully");

            cat = null;

        }
    }
}

我在您的页面中注意到,您没有使用 ADOX 对象执行任何操作,而是尝试使用改为 OleDBCommands。如果您要使用 ADOX,那么您应该使用 ADOX 对象创建表。

I must admit that I thought that ADO was really for C++ and VB not .NET technologies.

To quote from here

Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

On the same page you'll see the following code:

using System;
using ADOX;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            ADOX.CatalogClass cat = new ADOX.CatalogClass();

            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
                   "Data Source=D:\\AccessDB\\NewMDB.mdb;" +
                   "Jet OLEDB:Engine Type=5");

            Console.WriteLine("Database Created Successfully");

            cat = null;

        }
    }
}

I note in yours that you're not using the ADOX object to do anything and instead attempting to use OleDBCommands instead. If you're going to use ADOX then you should be creating the table using the ADOX object.

不必了 2024-12-24 17:35:21

仅当连接尚未打开时才应打开该连接。试试这个:

if (objOleDbConnection.State != ConnectionState.Open)
{
    objOleDbConnection.Open()
}

You should only open the connection if it is not already opened. Try this:

if (objOleDbConnection.State != ConnectionState.Open)
{
    objOleDbConnection.Open()
}
一念一轮回 2024-12-24 17:35:21

我不知道这是否适合你(从未使用过 ADOX)但是:
http://www.pcreview.co .uk/forums/fitting-access-file-created-adox-catalogclass-t1384766.html

tldr:

Marshal.ReleaseComObject(cat);
cat = null;

i don't know if this suits you (never used ADOX) but:
http://www.pcreview.co.uk/forums/closing-access-file-created-adox-catalogclass-t1384766.html

tldr:

Marshal.ReleaseComObject(cat);
cat = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文