-2
我在我的網站上使用這段代碼來獲取MySQL連接類。但通常連接每隔一小時或兩小時就會丟失。發生這種情況時,MySQL連接類不起作用,我的網站索引頁面無法顯示。我不知道這個問題的原因。網站無法顯示
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;
}
}
}