【问题标题】:Database Connection not closed when using Table Adapters使用表适配器时未关闭数据库连接
【发布时间】:2020-09-28 05:50:17
【问题描述】:

我有一个 Visual Studio (2008) c# 应用程序,我注意到在使用数据表适配器时它会保持数据库连接打开,直到应用程序关闭或大约 5 分钟的空闲时间过去。

我创建了一个简单的测试应用程序,除了 TableAdapter.Fill 之外没有其他任何东西 然后我在我的填充命令周围添加了一个 connection.open 和一个 connection.close ,但没​​有任何区别。

如果我无法强制关闭连接,是否可以将此超时缩短为 30 秒?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DatabaseConnectionTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //This line of code loads data into the 'insurvalDataSet.Building' table.
            this.buildingTableAdapter.Connection.Open();
            this.buildingTableAdapter.Fill(this.insurvalDataSet.Building);
            this.buildingTableAdapter.Connection.Close();
        }
    }
}

【问题讨论】:

  • @mahesh_b 我认为情况正好相反。他的查询运行时间过长,连接超时。在我用完之后,我的仍然保持打开状态。
  • 我正在尝试检查您提出的关于缩短适配器超时的问题!
  • @manesh,我想你误解了我的问题,但没关系。感谢您对它的破解:)

标签: c# visual-studio


【解决方案1】:

使用 using 进行连接并像这样关闭数据库...

使用 (MyClass mine = new MyClass()) { mine.Action(); }

【讨论】:

  • 我确实查看了 using 语句。我想以某种方式将它放在 FILL 线周围,但对我来说并没有多大意义。我不明白我将如何在我的示例代码中应用你的建议你能把它拼写清楚一点吗?
猜你喜欢
  • 1970-01-01
  • 2014-06-28
  • 2011-12-29
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多