using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using DataParameter =MySql.Data.MySqlClient.MySqlParameter; using DataCommand = MySql.Data.MySqlClient.MySqlCommand; using DataAdapter = MySql.Data.MySqlClient.MySqlDataAdapter; using ryCommonDb; using ryCommon; namespace ryCommonDb { /// /// /// public class MySqlDataProvider:IDbInterface { readonly ClsMySQLDb myDb = new ClsMySQLDb(); /// /// /// /// /// public int ConnDb(string sql) { var _sql = sql; if(_sql.IndexOfEx("Allow User Variables")<=0) { if (!_sql.EndsWith(";")) { _sql += ";"; } _sql += "Allow User Variables=True;"; } myDb.fv_ConnStr = _sql; return myDb.ConnDb(); } /// /// /// /// public int CloseDb() { myDb.CloseDb(); return 1; } /// /// /// /// /// /// public int GetCount(string tableName, string wheresql) { string m_where = ""; if (wheresql != "") { m_where = " where " + wheresql; } string sql = "select count(*) from " + tableName + m_where; return Convert.ToInt32(myDb.ExecuteSQL(sql, (DataParameter[])GetParameter(), "0")[0]); } /// /// /// /// /// public int ChangePwd(string newPwd) { return -1000; } private List list_param = new List(); /// /// /// /// /// public void AddParameter(string name,object value) { list_param.Add(new SQLIitem(name, value)); } /// /// /// /// /// public void ClearParameter(object name, object value) { list_param.Clear(); } /// /// /// /// public object[] GetParameter() { DataParameter[] defPar = new DataParameter[list_param.Count]; for (int i = 0; i < list_param.Count; i++) { SQLIitem item = (SQLIitem)list_param[i]; defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value); } return defPar; } /// /// /// /// /// public object[] GetParameter(RyQuickSQL mySQL) { DataParameter[] defPar = new DataParameter[mySQL.List.Count + mySQL.List_param.Count]; for (int i = 0; i < mySQL.List.Count; i++) { SQLIitem item = (SQLIitem)mySQL.List[i]; defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value); } for (int i = mySQL.List.Count; i < mySQL.List.Count + mySQL.List_param.Count; i++) { SQLIitem item = (SQLIitem)mySQL.List_param[i - mySQL.List.Count]; defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value); } return defPar; } /// /// /// /// /// /// /// public string GetPageSQL(string tableName, string wheresql, string orderSQL) { return GetPageSQL("*", tableName, wheresql, orderSQL); } /// /// /// /// /// /// /// /// public string GetPageSQL(string field, string tableName, string wheresql, string orderSQL) { string m_where = ""; if (wheresql != "") { m_where = " where " + wheresql; } string m_order = ""; if (orderSQL != "") { m_order = " " + orderSQL; } return "select " + field + " from " + tableName + m_where + m_order + " limit {recordnum},{pagesize}"; } /// /// /// /// /// /// /// public string GetPageSQL2(string tableName, string wheresql, string orderSQL) { return ""; } /// /// /// /// /// /// /// /// public string GetPageSQL2(string field, string tableName, string wheresql, string orderSQL) { return ""; } /// /// /// /// /// /// public bool ContainsData(string sql, object[] Parameter) { System.Data.DataSet ds = ReadData(sql, Parameter); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { return true; } return false; } /// /// /// /// /// /// public System.Data.DataSet ReadData(string sql, object[] Parameter) { DataCommand cmd = myDb.SQL_cn.CreateCommand(); cmd.Parameters.Clear(); cmd.CommandTimeout = 300; if (Parameter != null) { for (int i = 0; i < Parameter.Length; i++) { cmd.Parameters.Add(Parameter[i]); } } //cmd.Parameters.AddRange(Parameter); cmd.CommandText = sql; DataAdapter ad = new DataAdapter(cmd); System.Data.DataSet ds=new System.Data.DataSet(); ad.Fill(ds); ad.Dispose(); cmd.Parameters.Clear(); cmd.Dispose(); return ds; } /// /// /// /// /// /// public System.Data.DataSet ReadData(string sql, RyQuickSQL mySQL) { return ReadData(sql,GetParameter(mySQL)); } /// /// /// /// /// public System.Data.DataSet ReadData(string sql) { object[] Parameter = null; return ReadData(sql, Parameter); } /// /// /// /// /// /// public System.Data.DataSet ReadData(string tableName, string id) { object[] Parameter = null; return ReadData("select * from " + tableName + " where id=" + id, Parameter); } /// /// /// /// /// /// public int ExecuteNonQuery(string sql, object[] Parameter) { DataCommand cmd = myDb.SQL_cn.CreateCommand(); cmd.Parameters.Clear(); cmd.CommandTimeout = 300; if (Parameter!=null) cmd.Parameters.AddRange(Parameter); cmd.CommandText = sql; int i= cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); cmd.Dispose(); return i; } /// /// 执行SQL语句 /// /// /// 只针对IsAdd为false才生效,where 以后的sql语句 /// 是新增还是更新 /// public int ExecuteNonQuery(RyQuickSQL mySQL, string wheresql, bool IsAdd) { if (IsAdd) { return ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL); } else { if (wheresql.Length == 0) { return -1; } return ExecuteNonQuery(mySQL.GetUpdateSQL() + " where " + wheresql, mySQL); } } /// /// 插入数据库 /// /// /// public int Insert(RyQuickSQL mySQL) { return ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL); } /// /// 更新数据库 /// /// /// where 以后的sql语句 /// public int Update(RyQuickSQL mySQL, string wheresql) { if (wheresql.Length == 0) { return -1; } return ExecuteNonQuery(mySQL.GetUpdateSQL() + " where " + wheresql, mySQL); } /// /// /// /// /// /// public int ExecuteNonQuery(string sql, RyQuickSQL mySQL) { return ExecuteNonQuery(sql, GetParameter(mySQL)); } /// /// /// /// /// public int ExecuteNonQuery(string sql) { object[] pram = null; return ExecuteNonQuery(sql, pram); } /// /// /// /// /// /// public int DelById(string tableName, string id) { object[] param = null; return ExecuteNonQuery("delete from " + tableName + " where id=" + id, param); } /// /// /// /// /// public int CreateDb(RyQuickSQL mySQL) { string tmpSQL = "CREATE TABLE " + mySQL.TableName + " ([ID] INTEGER PRIMARY KEY,"; for (int i = 0; i < mySQL.List.Count; i++) { SQLIitem item = (SQLIitem)mySQL.List[i]; if (item.value is string) { if (item.len == 0) { tmpSQL += "[" + item.Field + "] [nvarchar](max),"; } else { tmpSQL += "[" + item.Field + "] [nvarchar]("+ item.len + "),"; } } else if (item.value is int || item.value is Int64) { tmpSQL += "[" + item.Field + "] [int] 0,"; } else if (item.value is double || item.value is float) { tmpSQL += "[" + item.Field + "] [float] 0,"; } else if (item.value is DateTime) { tmpSQL += "[" + item.Field + "] [DATETIME],"; } else if (item.value is byte[]) { tmpSQL += "[" + item.Field + "] [LONGBLOB],"; } } object[] param = null; return ExecuteNonQuery(tmpSQL.Substring(0, tmpSQL.Length - 1) + ")", param); } MySql.Data.MySqlClient.MySqlTransaction tr; /// /// /// public void BeginTransaction() { tr = myDb.SQL_cn.BeginTransaction(); } /// /// /// public void Commit() { tr.Commit(); } /// /// /// public void Free() { list_param.Clear(); myDb.CloseDb(); } } }