//--------------------------日期:2014-2-22 //--------------------------版本:1.0.0.0 //--------------------------作者:itrycn using System; using System.Collections.Generic; using System.Text; using System.Data; using SQLClient = MySql.Data.MySqlClient; using System.ComponentModel; namespace ryCommonDb { /// /// /// public class ClsMySQLDb : IDisposable { /// /// /// /// /// /// public delegate void ErrorHandler(object sender, string errorStr,string errorId); /// /// /// [Description("发生错误时发生")] public event ErrorHandler OnError; /// /// /// public SQLClient.MySqlConnection SQL_cn; /// /// /// public string fv_ConnStr = ""; /// /// /// public ClsMySQLDb() { } /// /// 连接数据库 /// /// 数据库连接字符串 /// public ClsMySQLDb(string ConnStr) { fv_ConnStr = ConnStr; } /// /// 连接数据库 /// /// 数据源 /// 数据库名称 /// 用户id /// 用户密码 public ClsMySQLDb(string DataSource, string DbName, string uId, string pwd) { if (uId == "") { fv_ConnStr = "Data Source=" + DataSource + ";database=" + DbName + ";Integrated Security=True;Pooling=False"; } else { fv_ConnStr = "Data Source=" + DataSource + ";database=" + DbName + ";uid=" + uId + ";pwd=" + pwd; } } /// /// /// ~ClsMySQLDb() { CloseDb(); } /// /// 连接数据库 /// /// public int ConnDb() { try { SQL_cn = new SQLClient.MySqlConnection { ConnectionString = fv_ConnStr }; SQL_cn.Open(); return 1; } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return -1; } } /// /// 关闭数据库 /// /// public void CloseDb() { try { if (SQL_cn != null && SQL_cn.State != ConnectionState.Closed) { SQL_cn.Close(); } } catch { OnError?.Invoke(this, "关闭数据库出错", "errorclose"); } } /// /// 运行SQL命令 /// /// SQL语句 /// SQL命令参数 /// 运行失败,则返回-1,否则返回影响的行数 public int ExecuteNonQuery(string SQLText, SQLClient.MySqlParameter[] commandParameters) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; if (commandParameters.Length > 0) { foreach (SQLClient.MySqlParameter parm in commandParameters) { cm.Parameters.Add(parm); } } cm.Connection = SQL_cn; int i = cm.ExecuteNonQuery(); cm.Dispose(); return i; } #endregion } catch (Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return -1; } } /// /// 运行SQL命令 /// /// SQL语句 /// 运行失败,则返回-1,否则返回影响的行数 public int ExecuteNonQuery(string SQLText) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; cm.Connection = SQL_cn; int i = cm.ExecuteNonQuery(); cm.Dispose(); return i; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errrorconn"); return -1; } } /// /// 运行SQL命令,并返回结果 /// /// SQL语句 /// SQL命令参数 /// 运行失败,则返回null,否则返回以数组显示的字符串 public string[] ExecuteSQL(string SQLText, SQLClient.MySqlParameter[] commandParameters) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; if (commandParameters.Length > 0) { cm.Parameters.AddRange(commandParameters); } SQLClient.MySqlDataReader reader = cm.ExecuteReader(); string[] resultStr = null; while (reader.Read()) { resultStr = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { resultStr[i] = reader[i].ToString(); } break; } reader.Close(); cm.Dispose(); return resultStr; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return null; } } /// /// 运行SQL命令,并返回结果 /// /// SQL语句 /// SQL命令参数 /// 数组第一个默认的值 /// 运行失败,则返回null,否则返回以数组显示的字符串 public string[] ExecuteSQL(string SQLText, SQLClient.MySqlParameter[] commandParameters, string DefFristValue) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; if (commandParameters.Length > 0) { cm.Parameters.AddRange(commandParameters); } SQLClient.MySqlDataReader reader = cm.ExecuteReader(); string[] resultStr = null; while (reader.Read()) { resultStr = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { resultStr[i] = reader[i].ToString(); } break; } if (resultStr == null) { resultStr = new string[1]; resultStr[0] = DefFristValue; } cm.Parameters.Clear(); reader.Close(); cm.Dispose(); return resultStr; } #endregion } catch(Exception ex) { string[] resultStr = null; resultStr = new string[1]; resultStr[0] = DefFristValue; OnError?.Invoke(this, ex.Message, "errorconn"); return resultStr; } } /// /// 运行SQL命令,并返回结果 /// /// SQL语句 /// 运行失败,则返回null,否则返回以数组显示的字符串 public string[] ExecuteSQL(string SQLText) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; SQLClient.MySqlDataReader reader = cm.ExecuteReader(); string[] resultStr = null; while (reader.Read()) { resultStr = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { resultStr[i] = reader[i].ToString(); } break; } reader.Close(); cm.Dispose(); return resultStr; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return null; } } /// /// 运行SQL命令,并返回结果 /// /// SQL语句 /// 数组第一个默认的值 /// 运行失败,则返回DefFristValue,否则返回以数组显示的字符串 public string[] ExecuteSQL(string SQLText, string DefFristValue) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; SQLClient.MySqlDataReader reader = cm.ExecuteReader(); string[] resultStr = null; while (reader.Read()) { resultStr = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { resultStr[i] = reader[i].ToString(); } break; } reader.Close(); cm.Dispose(); if (resultStr == null) { resultStr = new string[1]; resultStr[0] = DefFristValue; } return resultStr; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); string[] resultStr = null; resultStr = new string[1]; resultStr[0] = DefFristValue; return resultStr; } } /// /// 清空指定表的所有数据 /// /// 表名 /// 运行失败,则返回-1,否则返回影响的行数 public int ClearTableData(string TableName) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = "delete from " + TableName; cm.Connection = SQL_cn; int i = cm.ExecuteNonQuery(); cm.Dispose(); return i; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return -1; } } /// /// 判断指定值是否存在 /// /// 表名 /// 指定值所属字段 /// 指定值 /// 当前id,如果是新增记录,请填写-1 /// public bool IsExistValue(string TableName, string valueField, string value, int curId) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = "select * from " + TableName + " where " + valueField + "=@tvalue"; cm.Parameters.AddWithValue("@tvalue", DbType.String); cm.Parameters["@tvalue"].Value = value; SQLClient.MySqlDataReader reader = cm.ExecuteReader(); bool result = false; while (reader.Read()) { if (curId < 0) { result = true; } else { if (reader["id"].ToString() == curId.ToString()) { result = false; } else { result = true; } } break; } reader.Close(); cm.Dispose(); return result; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return false; } } /// /// 判断SQL语句是否有结果返回 /// /// SQL语句 /// SQL命令参数 /// 运行失败,则返回-1;存在结果,返回1;不存在结果,返回0 public int ExecuteReadResult(string SQLText, SQLClient.MySqlParameter[] commandParameters) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; if (commandParameters.Length > 0) { cm.Parameters.AddRange(commandParameters); //foreach (SQLiteParameter parm in commandParameters) //{ cm.Parameters.Add(parm); } } SQLClient.MySqlDataReader reader = cm.ExecuteReader(); int i = 0; while (reader.Read()) { i = 1; break; } reader.Close(); cm.Parameters.Clear(); cm.Dispose(); return i; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return -1; } } /// /// 判断SQL语句是否有结果返回 /// /// SQL语句 /// 运行失败,则返回-1;存在结果,返回1;不存在结果,返回0 public int ExecuteReadResult(string SQLText) { try { #region 数据库 { SQLClient.MySqlCommand cm = SQL_cn.CreateCommand(); cm.Parameters.Clear(); cm.CommandText = SQLText; SQLClient.MySqlDataReader reader = cm.ExecuteReader(); int i = 0; while (reader.Read()) { i = 1; break; } reader.Close(); cm.Dispose(); return i; } #endregion } catch(Exception ex) { OnError?.Invoke(this, ex.Message, "errorconn"); return -1; } } private bool disposed = false; /// /// /// public virtual void Dispose() { if (!this.disposed) { try { CloseDb(); // release scarce resource here } finally { this.disposed = true; GC.SuppressFinalize(this); } } } } }