public void Open() { if (_connect == null) { DBLOG.log(_connectionString); _connect = DbFactory.CreateDbConnection(_connectionString); (_connect as MySqlConnection).StateChange += conn_StateChange; _connect.Open(); } else if (state == ConnectionState.Closed) { _connect.Open(); } else if (state == ConnectionState.Broken) { _connect.Close(); _connect.Open(); } }
public List <T> Find <T>(string sql) where T : TbLogic, new() { List <T> list = new List <T>(); MySqlCommand cmd = new MySqlCommand(sql, (MySqlConnection)GetConnection()); DBLOG.log(sql); TableInfo dic = getInfo(typeof(T)); MySqlDataReader sdr = cmd.ExecuteReader(); try { while (sdr.Read()) { T entity = new T(); foreach (ColomnInfo co in dic.colomns.Values) { String name = co.columnName; if (CheckColumnName(sdr, name) == true) { if (sdr[name] is MySqlDateTime) { co.proptoty.SetValue(entity, ((MySqlDateTime)sdr[name]).GetDateTime(), null); } else { co.proptoty.SetValue(entity, sdr[name], null); } } } entity.changedKeys.Clear();//清理 list.Add(entity); } } finally { if (!sdr.IsClosed) { sdr.Close(); } } return(list); }
private int Execute(MySqlCommand cmd) { DBLOG.log(cmd.CommandText); IDbConnection conn = this.GetConnection(); cmd.Connection = conn as MySqlConnection; int re = 0; try { re = cmd.ExecuteNonQuery(); /* * StringBuilder str = new StringBuilder(); * //cmd.Parameters.RemoveAt("item"); * foreach (MySqlParameter o in cmd.Parameters) * { * str.Append(o.ParameterName).Append("=").Append(o.Value.ToString()).Append("&"); * } * DBLOG.log(str.ToString());*/ } catch (MySqlException mysqle) { DBLOG.error("出错3:", mysqle);// + JSON.Encode(cmd.Parameters),mysqle); StringBuilder str = new StringBuilder(); //cmd.Parameters.RemoveAt("item"); foreach (MySqlParameter o in cmd.Parameters) { str.Append(o.ParameterName).Append("=").Append(o.Value.ToString()); } DBLOG.error(str.ToString()); } catch (System.IO.IOException ioe) { DBLOG.error("出错4" + ioe.Message, ioe); conn.Close(); } return(re); }
private int Execute(string sql) { DBLOG.log("Execute:" + sql); IDbConnection conn = this.GetConnection(); int re = 0; MySqlCommand cmd = new MySqlCommand(sql, (MySqlConnection)conn); try { re = cmd.ExecuteNonQuery(); } catch (MySqlException mysqle) { DBLOG.error("出错3:", mysqle); } catch (System.IO.IOException ioe) { DBLOG.error("出错4", ioe); conn.Close(); } return(re); }
public int ExcuteSQL(string strSQL) { int val = 0; DBLOG.log("ExcuteSQL>" + strSQL); IDbConnection connection = null; try { //获取数据库连接,如果开启了事务,从事务中获取 connection = GetConnection(); val = new MySqlCommand(strSQL, connection as MySqlConnection).ExecuteNonQuery();// AdoHelper.ExecuteNonQuery(connection, null, CommandType.Text, strSQL); } catch (MySqlException mysqle) { DBLOG.error("出错ExcuteSQL:", mysqle); } catch (System.IO.IOException ioe) { DBLOG.error("出错5", ioe); connection.Close(); } return(val); }