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

发布于 2024-11-17 09:52:50 字数 1569 浏览 4 评论 0原文

无论如何,对 C# 来说相当新,我已经编写了这个初始化方法,它基本上创建了到 MS2007 Access 数据库的连接,用作为某些查询结果的 4 个数据表填充数据集。

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

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

以及初始化方法的开始,包括正在填充的数据表之一:

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;

有人能为我指明正确的方向吗?有任何提示或建议可以解决该连接问题吗?我一直像老板一样在谷歌上搜索,但似乎找不到我遇到的确切问题。

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.

    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.

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

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

发布评论

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

评论(2

挽清梦 2024-11-24 09:52:50

您还需要关闭连接。
在你的finally块上添加:

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

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

                    throw;
                }

            }
         }

问候

You also need to close your connection .
Add also on your finally block with:

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

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

                    throw;
                }

            }
         }

regards

我还不会笑 2024-11-24 09:52:50

此错误是由于保持连接打开而导致的。它不一定会立即发生,但总是在相同数量的请求之后发生。

我建议使用 using 语句包装您的 IDisposable 数据库类:

using (OleDbConnection con = new OleDbConnection())
{

}

这将自动调用 Dispose() 方法的实现并关闭您的连接。

来自 MSDN :“using 语句确保调用 Dispose,即使当你调用对象的方法时,你可以通过将对象放在 try 块中,然后在 finally 块中调用 Dispose 来实现相同的结果;事实上,这就是编译器翻译 using 语句的方式。 ...所以你最终不需要尝试捕获

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 (OleDbConnection con = new OleDbConnection())
{

}

This will automatically call the implementation on the Dispose() method and close your connections.

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." ... so you do not need your try catch finally

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