SqlConnection connection = new MySqlConnection(this.conn_str))
{
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand(sql, connection);
t = (T)cmd.ExecuteScalar();
connection.Close();
}
catch (Exception ex)
{
t = default(T);
logger.Warn("执行操作语句异常, SQL:" + sql, ex);
}
}
return t;
}
/// <summary>
/// 检查链接是否OK
/// </summary>
/// <returns></returns>
public bool Ping()
{
bool rest = false;
using (MySqlConnection connection = new MySqlConnection(this.conn_str))
{
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("select 1", connection);
cmd.ExecuteScalar();
connection.Close();
rest = true;
}
catch
{
rest = false;
}
}
return rest;
}
}
|