You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

211 lines
5.8 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using System.Data;
using MySql.Data.MySqlClient;
namespace Sog
{
public class MySqlDB : IDisposable
{
private string m_connectString;
private MySqlConnection m_mysqlConn;
private string m_db;
private string m_ip;
private string m_user;
public MySqlDB(string db, string ip, string user, string password)
{
m_db = db;
m_ip = ip;
m_user = user;
//经过实际测试,连接的时候需要设置utf8mb4且数据库字段的编码是utf8mb4才能正常写入4字节utf8字符,比如一些火星文,表情符
m_connectString = string.Format("Database='{0}';Data Source='{1}';User Id='{2}';Password='{3}';charset='utf8mb4';pooling=true;Allow User Variables=True;sslmode=none", db, ip, user, password);
}
//支持重连
public void OpenConn()
{
if (m_mysqlConn == null)
{
m_mysqlConn = new MySqlConnection(m_connectString);
}
try
{
m_mysqlConn.Open();
}
catch (Exception ex)
{
TraceLog.Error("connect mysql db {0} ip {1} user {2} error!", m_db, m_ip, m_user);
TraceLog.Exception(ex);
TraceLog.Error("Inner exception:");
TraceLog.Exception(ex.InnerException);
}
}
public void Dispose()
{
if (m_mysqlConn != null)
{
m_mysqlConn.Close();
m_mysqlConn.Dispose();
m_mysqlConn = null;
}
}
public MySqlDataReader ExecReader(string strSql)
{
// strSql = MySqlHelper.EscapeString(strSql);
MySqlDataReader reader = null;
try
{
if (m_mysqlConn == null || m_mysqlConn.State == ConnectionState.Closed)
{
OpenConn();
}
reader = MySqlHelper.ExecuteReader(m_mysqlConn, strSql);
//TraceLog.Trace("exec sql success {0} ", strSql);
return reader;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
if (reader != null)
{
reader.Close();
}
TraceLog.Error("MySqlDB.ExecReader throw error!");
//抛出异常,不往下执行了,因为执行结果未知,逻辑层根据返回空判断记录不存在,本身是错误的!
throw ex;
}
}
public MySqlDataReader ExecReader(string strSql, params MySqlParameter[] sqlParams)
{
//strSql = MySqlHelper.EscapeString(strSql);
MySqlDataReader reader = null;
try
{
if (m_mysqlConn == null || m_mysqlConn.State == ConnectionState.Closed)
{
OpenConn();
}
reader = MySqlHelper.ExecuteReader(m_mysqlConn, strSql, sqlParams);
//TraceLog.Trace("exec sql success {0} ", strSql);
return reader;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
if (reader != null)
{
reader.Close();
}
//这种情况怎么处理
TraceLog.Error("MySqlDB.ExecReader {0} throw error", strSql);
throw ex;
}
}
public bool ExecExist(string strSql, params MySqlParameter[] sqlParams)
{
var Reader = ExecReader(strSql, sqlParams);
var HasRows= Reader.HasRows;
Reader.Dispose();
return HasRows;
}
public int ExecNonQuery(string strSql, params MySqlParameter[] sqlParams)
{
// strSql = MySqlHelper.EscapeString(strSql);
try
{
if (m_mysqlConn == null || m_mysqlConn.State == ConnectionState.Closed)
{
OpenConn();
}
int ret = MySqlHelper.ExecuteNonQuery(m_mysqlConn, strSql, sqlParams);
//TraceLog.Trace("exec sql success {0}, ret {1}", strSql, ret);
return ret;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
//这种情况怎么处理
TraceLog.Error("MySqlDB.ExecNonQuery with params throw error");
throw ex;
}
}
public void TestQuery()
{
string testSql = "select * from accountId where accountId=10001";
MySqlCommand cmd = null;
//MySqlTransaction trans = null;
int count = 0;
try
{
if (m_mysqlConn == null || m_mysqlConn.State == ConnectionState.Closed)
{
OpenConn();
}
cmd = m_mysqlConn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = testSql;
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
int accountId = reader.GetInt32("accountId");
TraceLog.Trace("exec sql success {0} , accountId {1}", testSql, accountId);
}
}
//count = cmd.ExecuteNonQuery();
TraceLog.Trace("exec sql {0} effect count {1}", testSql, count);
}
catch
{
}
finally
{
}
}
}
}