0

I have a Visual Studio (2008) c# application and I have noticed that it leaves the database connection open when using data table adapters until the application is closed or until around 5 minutes of idle time has passed.

I have created a bare bones test application with nothing else in there except a TableAdapter.Fill I then added a connection.open and a connection.close around my fill command but it didn't make any difference.

If I can't force the connection to close can I shorten this timeout to say 30 seconds?

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();
        }
    }
}
David P
  • 411
  • 7
  • 21
  • Similar to this ? https://stackoverflow.com/questions/30910016/how-to-increase-timeout-of-dataadapter-to-3-min – MBB Jun 09 '20 at 06:21
  • @mahesh_b I think that is the other way around. His query is running too long and the connection is timing out. Mine are staying open after I have finished using them. – David P Jun 09 '20 at 07:00
  • I was trying to check the question you asked about shortening the time out on adapter! – MBB Jun 09 '20 at 09:17
  • @manesh, I think you missunderstood my question but that's OK. Thanks for having a crack at it :) – David P Jun 10 '20 at 07:32

1 Answers1

0

Use using for connection and Close Database as like this...

using (MyClass mine = new MyClass()) { mine.Action(); }

shafaetjsr
  • 314
  • 4
  • 11
  • I did look into the using statement. I thought about putting it around the FILL line somehow but it didn't make a lot of sence to me. I don't understand how I would apply your suggestion in my sample code Could you spell it out a bit clearer please? – David P Jun 10 '20 at 07:35