/// <summary> /// Execute the load operation /// </summary> /// <returns>The number of rows inserted.</returns> public int Load() { bool openedConnection = false; if (Connection == null) { throw new InvalidOperationException(Resources.ConnectionNotSet); } // next we open up the connetion if it is not already open if (connection.State != ConnectionState.Open) { openedConnection = true; connection.Open(); } try { string sql = BuildSqlCommand(); MyCatCommand cmd = new MyCatCommand(sql, Connection); cmd.CommandTimeout = Timeout; return(cmd.ExecuteNonQuery()); } finally { if (openedConnection) { connection.Close(); } } }
/// <summary> /// Executes a single command against a MySQL database. /// </summary> /// <param name="connectionString">Settings to use for this command</param> /// <param name="commandText">Command text to use</param> /// <param name="commandParameters">Array of <see cref="MyCatParameter"/> objects to use with the command</param> /// <returns><see cref="MyCatDataReader"/> object ready to read the results of the command</returns> public static MyCatDataReader ExecuteReader(string connectionString, string commandText, params MyCatParameter[] commandParameters) { //create & open a SqlConnection MyCatConnection cn = new MyCatConnection(connectionString); cn.Open(); //call the private overload that takes an internally owned connection in place of the connection string return(ExecuteReader(cn, null, commandText, commandParameters, false)); }
/// <summary> /// Executes a single command against a MySQL database. A new <see cref="MyCatConnection"/> is created /// using the <see cref="MyCatConnection.ConnectionString"/> given. /// </summary> /// <param name="connectionString"><see cref="MyCatConnection.ConnectionString"/> to use</param> /// <param name="commandText">SQL command to be executed</param> /// <param name="parms">Array of <see cref="MyCatParameter"/> objects to use with the command.</param> /// <returns></returns> public static int ExecuteNonQuery(string connectionString, string commandText, params MyCatParameter[] parms) { //create & open a SqlConnection, and dispose of it after we are done. using (MyCatConnection cn = new MyCatConnection(connectionString)) { cn.Open(); //call the overload that takes a connection in place of the connection string return(ExecuteNonQuery(cn, commandText, parms)); } }
/// <summary> /// Updates the given table with data from the given <see cref="DataSet"/> /// </summary> /// <param name="connectionString">Settings to use for the update</param> /// <param name="commandText">Command text to use for the update</param> /// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param> /// <param name="tablename">Tablename in the dataset to update</param> public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename) { MyCatConnection cn = new MyCatConnection(connectionString); cn.Open(); MyCatDataAdapter da = new MyCatDataAdapter(commandText, cn); MyCatCommandBuilder cb = new MyCatCommandBuilder(da); cb.ToString(); da.Update(ds, tablename); cn.Close(); }
public void CancelQuery(int timeout) { MyCatConnectionStringBuilder cb = new MyCatConnectionStringBuilder( Settings.ConnectionString); cb.Pooling = false; cb.AutoEnlist = false; cb.ConnectionTimeout = (uint)timeout; using (MyCatConnection c = new MyCatConnection(cb.ConnectionString)) { c.isKillQueryConnection = true; c.Open(); string commandText = "KILL QUERY " + ServerThread; MyCatCommand cmd = new MyCatCommand(commandText, c); cmd.CommandTimeout = timeout; cmd.ExecuteNonQuery(); } }
/// <summary> /// Executes this instance. /// </summary> /// <returns>The number of statements executed as part of the script.</returns> public int Execute() { bool openedConnection = false; if (connection == null) { throw new InvalidOperationException(Resources.ConnectionNotSet); } if (query == null || query.Length == 0) { return(0); } // next we open up the connetion if it is not already open if (connection.State != ConnectionState.Open) { openedConnection = true; connection.Open(); } // since we don't allow setting of parameters on a script we can // therefore safely allow the use of user variables. no one should be using // this connection while we are using it so we can temporarily tell it // to allow the use of user variables bool allowUserVars = connection.Settings.AllowUserVariables; connection.Settings.AllowUserVariables = true; try { string mode = connection.driver.Property("sql_mode"); mode = StringUtility.ToUpperInvariant(mode); bool ansiQuotes = mode.IndexOf("ANSI_QUOTES") != -1; bool noBackslashEscapes = mode.IndexOf("NO_BACKSLASH_ESCAPES") != -1; // first we break the query up into smaller queries List <ScriptStatement> statements = BreakIntoStatements(ansiQuotes, noBackslashEscapes); int count = 0; MyCatCommand cmd = new MyCatCommand(null, connection); foreach (ScriptStatement statement in statements) { if (String.IsNullOrEmpty(statement.text)) { continue; } cmd.CommandText = statement.text; try { cmd.ExecuteNonQuery(); count++; OnQueryExecuted(statement); } catch (Exception ex) { if (Error == null) { throw; } if (!OnScriptError(ex)) { break; } } } OnScriptCompleted(); return(count); } finally { connection.Settings.AllowUserVariables = allowUserVars; if (openedConnection) { connection.Close(); } } }