I use this code in my website for MySQL connection class.But generally connection is lost every per hour or two hours.when this happened, MySQL connection class don't work and my website index page can not be displayed. I don't know the reason for that problem.

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using MySql.Data.MySqlClient; 
using System.Configuration; 
using System.Data;

namespace hys
{
    public class dbIslem
    {

        MySqlConnection baglanti = new MySqlConnection(ConfigurationSettings.AppSettings["hys"].ToString());

        // işlem metodlarımız
        public void BaglantiAc()
        {
            try
            {
                //bağlantı durumu kontrol edilip kapalı ise açılır
                if (baglanti.State == ConnectionState.Closed)
                {
                    baglanti.Open(); 
                }
        else if(baglanti == null)
                {
                    baglanti.Close();
                    baglanti.Open();
                }

                // 
            }
            catch (Exception ex)
            {

                ex.Message.ToString();
            }

        }

        public void BaglantiKapat()
        {
            if (baglanti.State == ConnectionState.Open)
            {
                baglanti.Close();
        baglanti = null;

            }

        }

        public void BaglantiDurumu()
        {

            BaglantiAc();
            string baglantiDurumu = baglanti.State.ToString();
            BaglantiKapat();
        }

        public int KayitIslem(string sorguCumlesi)
        {
            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = sorgu.ExecuteNonQuery();
            baglanti.Close();
            return sonuc;

        }

        public int KayitEkle(string sorguCumlesi)
        {
            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = sorgu.ExecuteNonQuery();
            baglanti.Close();
            return sonuc;

        }

        public int KayitDuzenle(string sorguCumlesi)
        {
            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = sorgu.ExecuteNonQuery();
            baglanti.Close();
            return sonuc;

        }

        public int KayitSil(string sorguCumlesi)
        {
            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = sorgu.ExecuteNonQuery();
            baglanti.Close();
            return sonuc;
        }

        public int KayitScalar(string sorguCumlesi)
        {
            BaglantiAc();
            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = Convert.ToInt32(sorgu.ExecuteScalar());
            return sonuc;
        }

        public MySqlDataReader KayitGetir_DataReader(string sorguCumlesi)
        {

            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            MySqlDataReader dr = sorgu.ExecuteReader();
            return dr;

        }

        public DataSet KayitGetir_DataSet(string sorguCumlesi)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter dtAdapter = new MySqlDataAdapter();

            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);

            dtAdapter.SelectCommand = sorgu;

            try //hatali kayit varsa bile doldur, hata firlatma.
            {
                dtAdapter.Fill(ds);
            }
            catch
            {

            }

            return ds;
        }

        public DataTable KayitGetir_DataTable(string sorguCumlesi)
        {
            DataTable dt = new DataTable();
            MySqlDataAdapter dtAdapter = new MySqlDataAdapter();

            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);

            dtAdapter.SelectCommand = sorgu;
            dtAdapter.Fill(dt);
            return dt;

        }

        public string KayitAra(string sorguCumlesi)
        {
            BaglantiAc();

            MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
            int sonuc = sorgu.ExecuteNonQuery();

            //baglanti.Close();
            string sonucstring = Convert.ToString(sonuc);
            MySqlDataReader okuyucu = sorgu.ExecuteReader(CommandBehavior.CloseConnection);
            //Gelen datayı sonuc değişkenine aktar.    
            while(okuyucu.Read())    
             {
                 sonucstring = okuyucu.GetString(0);    
            }    
            okuyucu.Close();

            return sonucstring;

        }

    }

}

```