// Copyright © 2004, 2018, Oracle and/or its affiliates. All rights reserved. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License, version 2.0, as // published by the Free Software Foundation. // // This program is also distributed with certain software (including // but not limited to OpenSSL) that is licensed under separate terms, // as designated in a particular file or component or in included license // documentation. The authors of MySQL hereby grant you an // additional permission to link the program and your derivative works // with the separately licensed software that they have included with // MySQL. // // Without limiting anything contained in the foregoing, this file, // which is part of MySQL Connector/NET, is also subject to the // Universal FOSS Exception, version 1.0, a copy of which can be found at // http://oss.oracle.com/licenses/universal-foss-exception. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License, version 2.0, for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA using System; using System.Data; using System.Threading; using System.Threading.Tasks; namespace MySql.Data.MySqlClient { public partial class MySqlHelper { #region DataRow /// /// Asynchronous version of ExecuteDataRow. /// /// The settings to be used for the connection. /// The command to execute. /// The parameters to use for the command. /// The DataRow containing the first row of the resultset. public static Task ExecuteDataRowAsync(string connectionString, string commandText, params MySqlParameter[] parms) { return ExecuteDataRowAsync(connectionString, commandText, CancellationToken.None, parms); } /// /// Asynchronous version of ExecuteDataRow. /// /// The settings to be used for the connection. /// The command to execute. /// The cancellation token. /// The parameters to use for the command. /// The DataRow containing the first row of the resultset. public static Task ExecuteDataRowAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] parms) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var row = ExecuteDataRow(connectionString, commandText, parms); result.SetResult(row); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion #region DataSet /// /// Executes a single SQL command and returns the first row of the resultset. A new MySqlConnection object /// is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// DataRow containing the first row of the resultset public static DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms) { DataSet ds = ExecuteDataset(connectionString, commandText, parms); if (ds == null) { return null; } if (ds.Tables.Count == 0) { return null; } if (ds.Tables[0].Rows.Count == 0) { return null; } return ds.Tables[0].Rows[0]; } /// /// Executes a single SQL command and returns the resultset in a . /// A new MySqlConnection object is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// containing the resultset public static DataSet ExecuteDataset(string connectionString, string commandText) { // pass through the call providing null for the set of SqlParameters return ExecuteDataset(connectionString, commandText, (MySqlParameter[])null); } /// /// Executes a single SQL command and returns the resultset in a . /// A new MySqlConnection object is created, opened, and closed during this method. /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// containing the resultset public static DataSet ExecuteDataset(string connectionString, string commandText, params MySqlParameter[] commandParameters) { // create & open a SqlConnection, and dispose of it after we are done. using (MySqlConnection cn = new MySqlConnection(connectionString)) { cn.Open(); // call the overload that takes a connection in place of the connection string return ExecuteDataset(cn, commandText, commandParameters); } } /// /// Executes a single SQL command and returns the resultset in a . /// The state of the object remains unchanged after execution /// of this method. /// /// object to use /// Command to execute /// containing the resultset public static DataSet ExecuteDataset(MySqlConnection connection, string commandText) { // pass through the call providing null for the set of SqlParameters return ExecuteDataset(connection, commandText, (MySqlParameter[])null); } /// /// Executes a single SQL command and returns the resultset in a . /// The state of the object remains unchanged after execution /// of this method. /// /// object to use /// Command to execute /// Parameters to use for the command /// containing the resultset public static DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { // create a command and prepare it for execution MySqlCommand cmd = new MySqlCommand(); cmd.Connection = connection; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; if (commandParameters != null) { foreach (MySqlParameter p in commandParameters) { cmd.Parameters.Add(p); } } // create the DataAdapter & DataSet MySqlDataAdapter da = new MySqlDataAdapter(cmd); DataSet ds = new DataSet(); // fill the DataSet using default values for DataTable names, etc. da.Fill(ds); // detach the MySqlParameters from the command object, so they can be used again. cmd.Parameters.Clear(); // return the dataset return ds; } /// /// Updates the given table with data from the given /// /// Settings to use for the update /// Command text to use for the update /// containing the new data to use in the update /// Tablename in the dataset to update public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename) { MySqlConnection cn = new MySqlConnection(connectionString); cn.Open(); MySqlDataAdapter da = new MySqlDataAdapter(commandText, cn); MySqlCommandBuilder cb = new MySqlCommandBuilder(da); cb.ToString(); da.Update(ds, tablename); cn.Close(); } /// /// Async version of ExecuteDataset /// /// Settings to be used for the connection /// Command to execute /// containing the resultset public static Task ExecuteDatasetAsync(string connectionString, string commandText) { return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken) { return ExecuteDatasetAsync(connectionString, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteDataset /// /// Settings to be used for the connection /// Command to execute /// Parameters to use for the command /// containing the resultset public static Task ExecuteDatasetAsync(string connectionString, string commandText, params MySqlParameter[] commandParameters) { return ExecuteDatasetAsync(connectionString, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteDatasetAsync(string connectionString, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var dataset = ExecuteDataset(connectionString, commandText, commandParameters); result.SetResult(dataset); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of ExecuteDataset /// /// object to use /// Command to execute /// containing the resultset public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText) { return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, (MySqlParameter[])null); } public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken) { return ExecuteDatasetAsync(connection, commandText, cancellationToken, (MySqlParameter[])null); } /// /// Async version of ExecuteDataset /// /// object to use /// Command to execute /// Parameters to use for the command /// containing the resultset public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters) { return ExecuteDatasetAsync(connection, commandText, CancellationToken.None, commandParameters); } public static Task ExecuteDatasetAsync(MySqlConnection connection, string commandText, CancellationToken cancellationToken, params MySqlParameter[] commandParameters) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { var dataset = ExecuteDataset(connection, commandText, commandParameters); result.SetResult(dataset); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } /// /// Async version of UpdateDataset /// /// Settings to use for the update /// Command text to use for the update /// containing the new data to use in the update /// Tablename in the dataset to update public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename) { return UpdateDataSetAsync(connectionString, commandText, ds, tablename, CancellationToken.None); } public static Task UpdateDataSetAsync(string connectionString, string commandText, DataSet ds, string tablename, CancellationToken cancellationToken) { var result = new TaskCompletionSource(); if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested) { try { UpdateDataSet(connectionString, commandText, ds, tablename); result.SetResult(true); } catch (Exception ex) { result.SetException(ex); } } else { result.SetCanceled(); } return result.Task; } #endregion } }