我写了一个MySQL连接和查询类,他们都检查了Baglanti Class的Connected状态,但是这里没有更新的值是我的类。 这是Baglanti Class
abstract class Baglanti
{
protected MySqlConnection connDB
{ get; set; }
public bool Connected { get; set; }
}
连接类
class DB : Baglanti
{
private LogStream Writer { get; set; }
public void Baglan()
{
Writer = new LogStream();
var uid = "*****";
var pwd = "*****";
string connectionString;
connectionString = "SERVER=localhost;DATABASE=****;" + "UID=" + uid + ";" + "PASSWORD=" + pwd + ";";
connDB = new MySqlConnection(connectionString);
if (OpenConnection() == true)
{
base.Connected = true;
Writer.Info_Log("Veritabanı Bağlantısı Kuruldu.");
}
else { base.Connected = false; }
}
private bool OpenConnection()
{
try
{
connDB.Open();
base.Connected = true;
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Writer.Hata_Log("Veritabanına bağlantı kurulamadı.");
break;
case 1045:
Writer.Hata_Log("Veritabanı Kullanıcı-Şifre Hatası.");
break;
}
return false;
}
}
}
这是查询类
class Islem : Baglanti
{
private LogStream Writer { get; set; }
public Islem()
{
Writer = new LogStream();
}
public bool GirisKontrol(string id,string pw)
{
if(base.Connected)
{
MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM *** WHERE (**** = @user AND **** = @sifre)", connDB);
cmd.Parameters.AddWithValue("@user", id);
cmd.Parameters.AddWithValue("@sifre", pw);
int valid = int.Parse(cmd.ExecuteScalar().ToString());
if (valid > 0)
{ return true; }
else { return false; }
} else { Writer.Hata_Log("Veritabanı Bağlantısı Yok."); return false; }
}
}
我的主线程执行Baglan()void然后当我想从Islem执行GirisKontrol时,它总是返回false,而DB Connection不存在。我从公共布尔检查了在Baglanti Class连接