简体   繁体   English

C#-OleDbConnection.Open()导致崩溃

[英]C# - OleDbConnection.Open() causing a crash

Fairly new to C#, anyway, I have this Initialise method that I've written, it basically creates a connection to a MS2007 Access Database, fills a DataSet with the 4 DataTables that are the result of some queries. 无论如何,对于C#来说还算是新手,我已经编写了这个Initialize方法,它基本上创建了到MS2007 Access数据库的连接,并使用了一些查询结果的4个DataTable填充了DataSet。

    public frmDBCompareForm()
    {
        ///
        /// Required for Windows Form Design support
        ///
        InitializeComponent();
        frmDBCompareForm_Initialize();

        //
        // TODO: Add any constructor code
        //
        if (_InstancePtr == null) _InstancePtr = this;
    }

And the start of the Initialise Method, including one of the DataTables being filled: 初始化方法的开始,包括要填充的数据表之一:

private void frmDBCompareForm_Initialize()
    {
        // Fill DataSet with 3 DataTables, these tables will be 
        // made up of the from sQuery.
        try
        {
            // Create a new DataSet
            DataSet dsSite1 = new DataSet();
            // Set up the connection strings to HCAlias.accdb
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\HCAlias.accdb;Persist Security Info=False;";
            con.Open();
            //
            // Table 1 - dtSite1Name [cmbSite1]
            //
            dtSite1Name = new DataTable();
            string sQuery = "SELECT SourceName From Sites";
            OleDbCommand cmdSite1Name = new OleDbCommand(sQuery, con);
            OleDbDataAdapter myDASite1Name = new OleDbDataAdapter(cmdSite1Name);
            myDASite1Name.Fill(dsSite1, "dtSite1Name");
            cmbSite1.DataSource = dtSite1Name;
            cmbSite2.DataSource = dtSite1Name;

Could anyone point me in the right direction for going about this the way I have? 有人能指出我正确的方向吗? Any tips or advice to get that connection issue fixed? 有解决该连接问题的提示或建议吗? I've been Googling like a boss, but can't seem to find the exact issue that I'm having. 我一直像老板一样谷歌搜索,但是似乎找不到我遇到的确切问题。

You also need to close your connection . 您还需要关闭连接。 Add also on your finally block with: 在您的finally块中添加以下内容:

        using (var con = new OleDbConnection())
        {
            con.Open();
            using (var cmd = new OleDbCommand("sqlquery", conn))
            {

                try
                {
                             //do Stuff here
                }
                catch (OleDbException)
                {

                    throw;
                }

            }
         }

regards 问候

This error is caused by leaving connections open. 此错误是由于连接保持打开状态引起的。 It won't necessarily happen right away but always after the same number of requests. 它不一定会立即发生,而总是在相同数量的请求之后发生。

I suggest wrapping your IDisposable db classes with using statements: 我建议使用using语句包装您的IDisposabledb类:

using (OleDbConnection con = new OleDbConnection())
{

}

This will automatically call the implementation on the Dispose() method and close your connections. 这将自动在Dispose()方法上调用实现并关闭您的连接。

From MSDN : "The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler." 在MSDN中 :“ using语句确保即使在对象上调用方法时发生异常,都将调用Dispose。通过将对象放在try块中,然后在finally块中调用Dispose,可以实现相同的结果;在实际上,这就是编译器翻译using语句的方式。” ... so you do not need your try catch finally ...所以您最终不需要尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM