错误告诉我我还没有关闭连接,但不是吗?
我一定是在这里错过了什么。我正在尝试创建一个表,但收到一条错误消息,告诉我连接仍处于打开状态。但在哪里???我已经重新阅读了代码,但我找不到连接仍然打开的位置...
问题出在这里: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来 ADOX 对象也应该关闭它的连接。
请看以下来自 Microsoft 的示例:
http://msdn.microsoft。 com/en-us/library/windows/desktop/ms681562(v=vs.85).aspx
他们的 ADOX 对象名为 cat。
它们具有以下内容:
这可能会翻译为:
在您的代码中(可能在第一个 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:
Which will probably translate to:
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.
我必须承认,我认为 ADO 实际上适用于 C++ 和 VB,而不是 .NET 技术。
引用此处
在同一页面上,您将看到以下代码:
我在您的页面中注意到,您没有使用 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
On the same page you'll see the following code:
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.
仅当连接尚未打开时才应打开该连接。试试这个:
You should only open the connection if it is not already opened. Try this:
我不知道这是否适合你(从未使用过 ADOX)但是:
http://www.pcreview.co .uk/forums/fitting-access-file-created-adox-catalogclass-t1384766.html
tldr:
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: