将OleDbConnection与OpenFileDialog一起使用时出现AccessViolationException [英] AccessViolationException when using OleDbConnection with OpenFileDialog
问题描述
我创建了一个带有静态类的数据层,该类具有用于从数据库读取的静态方法(访问2010).在我的应用程序中,我有2个按钮,一个用于将某些内容(在List中)保存到数据库文件(Reports.accdb)中,一个用于显示 一个OpenFileDialog.
I have created a datalayer with a static class with static methods for reading from a database (access 2010). In my application I have 2 buttons, one for saving some content (in a List<>) to a databasefile (Reports.accdb) and one for displaying an OpenFileDialog.
如果单击SaveToDb按钮,然后单击调用OpenFileDialog.ShowDialog()的按钮,则会收到AccessViolationException.另一方面,如果我首先单击OpenFileDialog.ShowDialog(),然后单击SaveToDb-Button,则不会出现异常.
If I click the SaveToDb-button and then click the button calling OpenFileDialog.ShowDialog() I get an AccessViolationException. If I on the other hand start with clicking OpenFileDialog.ShowDialog() first and then the SaveToDb-button I don't get the Exception.
那是为什么?
如果需要,我可以发布更多代码.下面是一些代码.
If needed I can post more code. Below are some of the code.
在数据层中
using(OleDbConnection conn = new OleDbConnection("
using(OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\Reports.accdb;Persist Security Info=False"))
{
//Code for writing things to db
}
在Forms-app中
In Forms-app
btn1_Click()
{
DataLayer.WriteToDb(myList);
}
btn1_Click()
{
DataLayer.WriteToDb(myList);
}
btn2_Click()
{
myOpenFileDialog.ShowDialog();
}
btn2_Click()
{
myOpenFileDialog.ShowDialog();
}
推荐答案
DAL和UI的线程COM公寓是什么?
What is the thread' COM apartment of your DAL and your UI?
这篇关于将OleDbConnection与OpenFileDialog一起使用时出现AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!