/// <summary> /// 执行SQL语句返回结果到DataTable中 /// </summary> /// <param name="strSQL"></param> /// <returns></returns> public DataTable ExecuteDataTable(string strSQL) { int begintime = Environment.TickCount; databaseMutex.WaitOne(); DataTable table; try { if (mysqlConnection.State == ConnectionState.Closed) { mysqlConnection.Open(); } table = new DataTable(); MySqlDataAdapter dbda = new MySqlDataAdapter(strSQL, mysqlConnection); dbda.Fill(table); } catch (Exception e) { CLog4net.LogError(strSQL + " " + e); return(null); } finally { databaseMutex.ReleaseMutex(); mysqlConnection.Close(); } CLog4net.LogInfo(strSQL + " 执行结束:" + (Environment.TickCount - begintime) + "ms"); return(table); }
public int MysqlCommand(string MysqlCommand) { try { mysqlConnection.Open(); Console.WriteLine("MysqlConnection Opened."); MySqlCommand mysqlCommand = new MySqlCommand(MysqlCommand, mysqlConnection); return(mysqlCommand.ExecuteNonQuery()); } catch (MySqlException ex) { CLog4net.LogError("MySqlException Error:" + ex); if (Regex.IsMatch(ex.ToString(), "")) { CLog4net.LogError("数据库已经存在唯一键值"); } } finally { mysqlConnection.Close(); } return(-1); }
public string MysqlInfo() { string mysqlInfo = null; try { mysqlConnection.Open(); mysqlInfo += "Connection Opened." + Environment.NewLine; mysqlInfo += "Connection String:" + mysqlConnection.ConnectionString.ToString() + Environment.NewLine; mysqlInfo += "Database:" + mysqlConnection.Database.ToString() + Environment.NewLine; mysqlInfo += "Connection ServerVersion:" + mysqlConnection.ServerVersion.ToString() + Environment.NewLine; mysqlInfo += "Connection State:" + mysqlConnection.State.ToString() + Environment.NewLine; } catch (MySqlException ex) { CLog4net.LogError("MySqlException Error:" + ex); } finally { mysqlConnection.Close(); } return(mysqlInfo); }
/// <summary> /// 执行SQL语句没有返回结果,如:执行删除、更新、插入等操作 /// </summary> /// <param name="strSQL"></param> /// <returns>操作成功标志</returns> public bool ExecuteNonQuery(string strSQL) { int begintime = Environment.TickCount; databaseMutex.WaitOne(); bool resultState = false; if (mysqlConnection.State != ConnectionState.Open) { mysqlConnection.Open(); } MySqlTransaction myTrans = mysqlConnection.BeginTransaction();//不支持并行用户 MySqlCommand command = new MySqlCommand(strSQL, mysqlConnection, myTrans); try { command.ExecuteNonQuery(); myTrans.Commit(); resultState = true; } catch (Exception e) { myTrans.Rollback(); resultState = false; CLog4net.LogError(strSQL + " " + e); } finally { databaseMutex.ReleaseMutex(); mysqlConnection.Close(); } CLog4net.LogInfo(strSQL + " 执行结束:" + (Environment.TickCount - begintime) + "ms"); return(resultState); }