我有一个Visual Studio (2008) c#应用程序,我注意到,当使用数据表适配器时,它将数据库连接打开,直到应用程序关闭或直到大约5分钟的空闲时间过去。
我已经创建了一个赤裸裸的测试应用程序,里面除了一个TableAdapter.FillI之外没有其他东西,然后在我的填充命令周围添加了一个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();
}
}
}
使用using来连接和关闭数据库,就像这样...。
using (MyClass mine = new MyClass()) { mine.Action(); }。