我在我的网站上使用此代码来获取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;
}
}
}
答案 0 :(得分:1)
您应始终关闭连接。而不是做例如。
BaglantiAc();
MySqlCommand sorgu = new MySqlCommand(sorguCumlesi, baglanti);
int sonuc = sorgu.ExecuteNonQuery();
baglanti.Close();
return sonuc;
待办事项
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
MySqlCommand command = new MySqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
using
语句将调用MySqlConnection.Dispose()和MySqlConnection.Close()
答案 1 :(得分:0)
使用类dbIslem实现IDisposable接口并以这种方式覆盖Dispose()方法。
public class dbIslem:IDisposable
{
public void Dispose()
{
baglanti.Dispose();
command.Dispose();
}
}