//****************************************************************************************************** // DataExtensions.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 02/05/2003 - J. Ritchie Carroll // Generated original version of source code. // 05/25/2004 - J. Ritchie Carroll // Added "with parameters" overloads to all basic query functions. // 12/10/2004 - Tim M Shults // Added several new WithParameters overloads that allow a programmer to send just the // parameter values instead of creating a series of parameter objects and then sending // them through. Easy way to cut down on the amount of code. // This code is just for calls to Stored Procedures and will not work for in-line SQL. // 03/28/2006 - Pinal C. Patel // Migrated 2.0 version of source code from 1.1 source (GSF.Database.Common). // 08/21/2007 - Darrell Zuercher // Edited code comments. // 09/15/2008 - J. Ritchie Carroll // Converted to C# extensions. // 09/29/2008 - Pinal C. Patel // Reviewed code comments. // 09/09/2009 - J. Ritchie Carroll // Added extensions for ODBC providers. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 12/02/2009 - Stephen C. Wills // Added disposal of database command objects. // 09/28/2010 - J. Ritchie Carroll // Added Stephen's CreateParameterizedCommand connection extension. // 04/07/2011 - J. Ritchie Carroll // Added Mehul's AddParameterWithValue command extension. Added overloads for all // PopulateParameters() command extensions so that they could take a "params" style // array of values after initial value for ease-of-use. Added "params" style array // to all templated IDbConnection that will use the CreateParameterizedCommand // connection extension with optional parameters. // 06/16/2011 - Pinal C. Patel // Modified AddParameterWithValue() to be backwards compatible. // 07/18/2011 - Stephen C. Wills // Added DataRow extension functions to automatically convert from types that // implement the IConvertible interface. // 08/12/2011 - Pinal C. Patel // Modified AddParameterWithValue() to correctly implement backwards compatible. // 09/19/2011 - Stephen C. Wills // Modified AddParametersWithValues() to parse parameters prefixed // with a colon for Oracle database compatibility. // 09/21/2011 - J. Ritchie Carroll // Added Mono implementation exception regions. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Data; using System.Data.Common; using System.Data.Odbc; using System.Data.OleDb; using System.Data.SqlClient; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; namespace GSF.Data { // ReSharper disable UnusedVariable /// /// Defines extension functions related to database and SQL interaction. /// public static class DataExtensions { /// /// The default timeout duration used for executing SQL statements when timeout duration is not specified. /// public const int DefaultTimeoutDuration = 30; // Defines a list of keywords used to identify PL/SQL blocks. private static readonly string[] s_plsqlIdentifiers = { "CREATE FUNCTION", "CREATE OR REPLACE FUNCTION", "CREATE PROCEDURE", "CREATE OR REPLACE PROCEDURE", "CREATE PACKAGE", "CREATE OR REPLACE PACKAGE", "DECLARE", "BEGIN" }; private static readonly Regex s_sqlParameterRegex; private static readonly Regex s_sqlCommentRegex; private static readonly Regex s_sqlIdentifierRegex; static DataExtensions() { s_sqlParameterRegex = new Regex(@"^[:@][a-zA-Z]\w*$", RegexOptions.Compiled); s_sqlCommentRegex = new Regex(@"/\*.*\*/|--.*(?=\n)", RegexOptions.Multiline); s_sqlIdentifierRegex = new Regex(@"^(\S+|(\S+|\[.+\])(\.\S+|\.\[.+\])*|(\S+|\`.+\`)(\.\S+|\.\`.+\`)*|(\S+|\"".+\"")(\.\S+|\.\"".+\"")*)$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture); } #region [ SQL Encoding String Extension ] /// /// Performs SQL encoding on given SQL string. /// /// The string on which SQL encoding is to be performed. /// Database type for the SQL encoding. /// The SQL encoded string. public static string SQLEncode(this string sql, DatabaseType databaseType = DatabaseType.Other) { if (databaseType == DatabaseType.MySQL) return sql.Replace("\\", "\\\\").Replace("\'", "\\\'"); return sql.Replace("\'", "\'\'"); //.Replace("/*", "").Replace("--", ""); } #endregion #region [ ExecuteNonQuery Overloaded Extension ] /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The number of rows affected. /// Type of to use. public static int ExecuteNonQuery(this TConnection connection, string sql, params object[] parameters) where TConnection : IDbConnection { return connection.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The number of rows affected. /// Type of to use. public static int ExecuteNonQuery(this TConnection connection, string sql, int timeout, params object[] parameters) where TConnection : IDbConnection { using (IDbCommand command = connection.CreateParameterizedCommand(sql, parameters)) { command.CommandTimeout = timeout; return command.ExecuteNonQuery(); } } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OleDbConnection connection, string sql, params object[] parameters) { return connection.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int ExecuteNonQuery(this OleDbConnection connection, string sql, int timeout, params object[] parameters) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OdbcConnection connection, string sql, params object[] parameters) { return connection.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int ExecuteNonQuery(this OdbcConnection connection, string sql, int timeout, params object[] parameters) { using (OdbcCommand command = new OdbcCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this SqlConnection connection, string sql, params object[] parameters) { return connection.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int ExecuteNonQuery(this SqlConnection connection, string sql, int timeout, params object[] parameters) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The number of rows affected. public static int ExecuteNonQuery(this IDbCommand command, string sql, params object[] parameters) { return command.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The number of rows affected. public static int ExecuteNonQuery(this IDbCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.AddParametersWithValues(sql, parameters); return command.ExecuteNonQuery(); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OleDbCommand command, string sql, params object[] parameters) { return command.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OleDbCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OdbcCommand command, string sql, params object[] parameters) { return command.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this OdbcCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this SqlCommand command, string sql, params object[] parameters) { return command.ExecuteNonQuery(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the number of rows affected. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The number of rows affected. public static int ExecuteNonQuery(this SqlCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteNonQuery(); } #endregion #region [ ExecuteReader Overloaded Extensions ] /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. /// Type of to use. public static IDataReader ExecuteReader(this TConnection connection, string sql, params object[] parameters) where TConnection : IDbConnection { return connection.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. /// Type of to use. public static IDataReader ExecuteReader(this TConnection connection, string sql, int timeout, params object[] parameters) where TConnection : IDbConnection { return connection.ExecuteReader(sql, CommandBehavior.Default, timeout, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. /// Type of to use. public static IDataReader ExecuteReader(this TConnection connection, string sql, CommandBehavior behavior, int timeout, params object[] parameters) where TConnection : IDbConnection { using (IDbCommand command = connection.CreateParameterizedCommand(sql, parameters)) { command.CommandTimeout = timeout; return command.ExecuteReader(behavior); } } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OleDbDataReader ExecuteReader(this OleDbConnection connection, string sql, params object[] parameters) { return connection.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static OleDbDataReader ExecuteReader(this OleDbConnection connection, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OdbcDataReader ExecuteReader(this OdbcConnection connection, string sql, params object[] parameters) { return connection.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static OdbcDataReader ExecuteReader(this OdbcConnection connection, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { using (OdbcCommand command = new OdbcCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static SqlDataReader ExecuteReader(this SqlConnection connection, string sql, params object[] parameters) { return connection.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static SqlDataReader ExecuteReader(this SqlConnection connection, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static IDataReader ExecuteReader(this IDbCommand command, string sql, params object[] parameters) { return command.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static IDataReader ExecuteReader(this IDbCommand command, string sql, int timeout, params object[] parameters) { return command.ExecuteReader(sql, CommandBehavior.Default, timeout, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static IDataReader ExecuteReader(this IDbCommand command, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.AddParametersWithValues(sql, parameters); return command.ExecuteReader(behavior); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OleDbDataReader ExecuteReader(this OleDbCommand command, string sql, params object[] parameters) { return command.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OleDbDataReader ExecuteReader(this OleDbCommand command, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OdbcDataReader ExecuteReader(this OdbcCommand command, string sql, params object[] parameters) { return command.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static OdbcDataReader ExecuteReader(this OdbcCommand command, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static SqlDataReader ExecuteReader(this SqlCommand command, string sql, params object[] parameters) { return command.ExecuteReader(sql, CommandBehavior.Default, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and builds a . /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// One of the values. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static SqlDataReader ExecuteReader(this SqlCommand command, string sql, CommandBehavior behavior, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteReader(behavior); } #endregion #region [ ExecuteScalar Overloaded Extensions ] /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// Value in the first column of the first row in the result set. /// Type of to use. public static object ExecuteScalar(this TConnection connection, string sql, params object[] parameters) where TConnection : IDbConnection { return connection.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// Value in the first column of the first row in the result set. /// Type of to use. public static object ExecuteScalar(this TConnection connection, string sql, int timeout, params object[] parameters) where TConnection : IDbConnection { using (IDbCommand command = connection.CreateParameterizedCommand(sql, parameters)) { command.CommandTimeout = timeout; return command.ExecuteScalar(); } } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OleDbConnection connection, string sql, params object[] parameters) { return connection.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static object ExecuteScalar(this OleDbConnection connection, string sql, int timeout, params object[] parameters) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteScalar(); } } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OdbcConnection connection, string sql, params object[] parameters) { return connection.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static object ExecuteScalar(this OdbcConnection connection, string sql, int timeout, params object[] parameters) { using (OdbcCommand command = new OdbcCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteScalar(); } } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this SqlConnection connection, string sql, params object[] parameters) { return connection.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static object ExecuteScalar(this SqlConnection connection, string sql, int timeout, params object[] parameters) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); return command.ExecuteScalar(); } } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this IDbCommand command, string sql, params object[] parameters) { return command.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this IDbCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.AddParametersWithValues(sql, parameters); return command.ExecuteScalar(); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OleDbCommand command, string sql, params object[] parameters) { return command.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OleDbCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteScalar(); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OdbcCommand command, string sql, params object[] parameters) { return command.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this OdbcCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteScalar(); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this SqlCommand command, string sql, params object[] parameters) { return command.ExecuteScalar(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the value in the first column /// of the first row in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// Value in the first column of the first row in the result set. public static object ExecuteScalar(this SqlCommand command, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); return command.ExecuteScalar(); } #endregion #region [ ExecuteScript Overloaded Extensions ] /// /// Executes the statements defined in the given TSQL script. /// /// The connection used to execute SQL statements. /// The path to the SQL script. public static void ExecuteTSQLScript(this IDbConnection connection, string scriptPath) { using (TextReader scriptReader = File.OpenText(scriptPath)) { ExecuteTSQLScript(connection, scriptReader); } } /// /// Executes the statements defined in the given TSQL script. /// /// The connection used to execute SQL statements. /// The reader used to extract statements from the SQL script. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities", Justification = "CommandText updates are being removed, not accepting user input")] public static void ExecuteTSQLScript(this IDbConnection connection, TextReader scriptReader) { string line = scriptReader.ReadLine(); using (IDbCommand command = connection.CreateCommand()) { StringBuilder statementBuilder = new StringBuilder(); while ((object)line != null) { string trimLine = line.Trim(); string statement; if (trimLine == "GO") { // Remove comments and execute the statement. statement = statementBuilder.ToString(); command.CommandText = s_sqlCommentRegex.Replace(statement, " ").Trim(); command.ExecuteNonQuery(); statementBuilder.Clear(); } else { // Append this line to the statement statementBuilder.Append(line); statementBuilder.Append('\n'); } // Read the next line from the file. line = scriptReader.ReadLine(); } } } /// /// Executes the statements defined in the given MySQL script. /// /// The connection used to execute SQL statements. /// The path to the SQL script. public static void ExecuteMySQLScript(this IDbConnection connection, string scriptPath) { using (TextReader scriptReader = File.OpenText(scriptPath)) { ExecuteMySQLScript(connection, scriptReader); } } /// /// Executes the statements defined in the given MySQL script. /// /// The connection used to execute SQL statements. /// The reader used to extract statements from the SQL script. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities", Justification = "CommandText updates are being removed, not accepting user input")] public static void ExecuteMySQLScript(this IDbConnection connection, TextReader scriptReader) { string line; string delimiter; line = scriptReader.ReadLine(); delimiter = ";"; using (IDbCommand command = connection.CreateCommand()) { StringBuilder statementBuilder = new StringBuilder(); while ((object)line != null) { string statement; if (line.StartsWith("DELIMITER ", StringComparison.OrdinalIgnoreCase)) { delimiter = line.Split(' ')[1].Trim(); } else { statementBuilder.Append(line); statementBuilder.Append('\n'); statement = statementBuilder.ToString(); statement = s_sqlCommentRegex.Replace(statement, " ").Trim(); if (statement.EndsWith(delimiter, StringComparison.Ordinal)) { // Remove trailing delimiter. statement = statement.Remove(statement.Length - delimiter.Length); // Remove comments and execute the statement. command.CommandText = statement; command.ExecuteNonQuery(); statementBuilder.Clear(); } } // Read the next line from the file. line = scriptReader.ReadLine(); } } } /// /// Executes the statements defined in the given Oracle database script. /// /// The connection used to execute SQL statements. /// The path to the SQL script. public static void ExecuteOracleScript(this IDbConnection connection, string scriptPath) { using (TextReader scriptReader = File.OpenText(scriptPath)) { ExecuteOracleScript(connection, scriptReader); } } /// /// Executes the statements defined in the given Oracle database script. /// /// The connection used to execute SQL statements. /// The reader used to extract statements from the SQL script. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities", Justification = "CommandText updates are being removed, not accepting user input")] public static void ExecuteOracleScript(this IDbConnection connection, TextReader scriptReader) { string line; line = scriptReader.ReadLine(); using (IDbCommand command = connection.CreateCommand()) { StringBuilder statementBuilder = new StringBuilder(); while ((object)line != null) { string trimLine = line.Trim(); string statement; bool isPlsqlBlock; statementBuilder.Append(line); statementBuilder.Append('\n'); statement = statementBuilder.ToString(); statement = s_sqlCommentRegex.Replace(statement, " ").Trim(); // Determine whether the statement is a PL/SQL block. // If the statement is a PL/SQL block, the delimiter // is a forward slash. Otherwise, it is a semicolon. isPlsqlBlock = s_plsqlIdentifiers.Any(ident => statement.IndexOf(ident, StringComparison.CurrentCultureIgnoreCase) >= 0); // If the statement is a PL/SQL block and the current line is a forward slash, // or if the statement is not a PL/SQL block and the statement in a semicolon, // then execute and flush the statement so that the next statement can be executed. if ((isPlsqlBlock && trimLine == "/") || (!isPlsqlBlock && statement.EndsWith(";", StringComparison.Ordinal))) { // Remove trailing delimiter and newlines. statement = statement.Remove(statement.Length - 1); // Remove comments and execute the statement. command.CommandText = statement; command.ExecuteNonQuery(); statementBuilder.Clear(); } // Read the next line from the file. line = scriptReader.ReadLine(); } } } #endregion #region [ RetrieveRow Overloaded Extensions ] /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this OleDbConnection connection, string sql) { return connection.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this OleDbConnection connection, string sql, int timeout) { return connection.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OleDbConnection connection, string sql, params object[] parameters) { return connection.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OleDbConnection connection, string sql, int timeout, params object[] parameters) { DataTable dataTable = connection.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this OdbcConnection connection, string sql) { return connection.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this OdbcConnection connection, string sql, int timeout) { return connection.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OdbcConnection connection, string sql, params object[] parameters) { return connection.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OdbcConnection connection, string sql, int timeout, params object[] parameters) { DataTable dataTable = connection.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this SqlConnection connection, string sql) { return connection.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this SqlConnection connection, string sql, int timeout) { return connection.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this SqlConnection connection, string sql, params object[] parameters) { return connection.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this SqlConnection connection, string sql, int timeout, params object[] parameters) { DataTable dataTable = connection.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The first in the result set. public static DataRow RetrieveRow(this IDbConnection connection, Type dataAdapterType, string sql, params object[] parameters) { return connection.RetrieveRow(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The first in the result set. public static DataRow RetrieveRow(this IDbConnection connection, Type dataAdapterType, string sql, int timeout, params object[] parameters) { DataTable dataTable = connection.RetrieveData(dataAdapterType, sql, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this OleDbCommand command, string sql) { return command.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this OleDbCommand command, string sql, int timeout) { return command.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OleDbCommand command, string sql, params object[] parameters) { return command.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OleDbCommand command, string sql, int timeout, params object[] parameters) { DataTable dataTable = command.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this OdbcCommand command, string sql) { return command.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this OdbcCommand command, string sql, int timeout) { return command.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OdbcCommand command, string sql, params object[] parameters) { return command.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this OdbcCommand command, string sql, int timeout, params object[] parameters) { DataTable dataTable = command.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The first in the result set. public static DataRow RetrieveRow(this SqlCommand command, string sql) { return command.RetrieveRow(sql, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The first in the result set. public static DataRow RetrieveRow(this SqlCommand command, string sql, int timeout) { return command.RetrieveRow(sql, timeout, null); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this SqlCommand command, string sql, params object[] parameters) { return command.RetrieveRow(sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// The first in the result set. public static DataRow RetrieveRow(this SqlCommand command, string sql, int timeout, params object[] parameters) { DataTable dataTable = command.RetrieveData(sql, 0, 1, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The first in the result set. public static DataRow RetrieveRow(this IDbCommand command, Type dataAdapterType, string sql, params object[] parameters) { return command.RetrieveRow(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first in the result set. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// The first in the result set. public static DataRow RetrieveRow(this IDbCommand command, Type dataAdapterType, string sql, int timeout, params object[] parameters) { DataTable dataTable = command.RetrieveData(dataAdapterType, sql, timeout, parameters); if (dataTable.Rows.Count == 0) dataTable.Rows.Add(dataTable.NewRow()); return dataTable.Rows[0]; } #endregion #region [ RetrieveData Overloaded Extensions ] /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this OleDbConnection connection, string sql) { return connection.RetrieveData(sql, 0, int.MaxValue, 30); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this OleDbConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OleDbConnection connection, string sql, params object[] parameters) { return connection.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OleDbConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this OdbcConnection connection, string sql) { return connection.RetrieveData(sql, 0, int.MaxValue, 30); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this OdbcConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OdbcConnection connection, string sql, params object[] parameters) { return connection.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OdbcConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this SqlConnection connection, string sql) { return connection.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this SqlConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this SqlConnection connection, string sql, params object[] parameters) { return connection.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this SqlConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataTable RetrieveData(this IDbConnection connection, Type dataAdapterType, string sql, params object[] parameters) { return connection.RetrieveData(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataTable RetrieveData(this IDbConnection connection, Type dataAdapterType, string sql, int timeout, params object[] parameters) { return connection.RetrieveDataSet(dataAdapterType, sql, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this OleDbCommand command, string sql) { return command.RetrieveData(sql, 0, int.MaxValue, 30); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this OleDbCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OleDbCommand command, string sql, params object[] parameters) { return command.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OleDbCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this OdbcCommand command, string sql) { return command.RetrieveData(sql, 0, int.MaxValue, 30); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this OdbcCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OdbcCommand command, string sql, params object[] parameters) { return command.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this OdbcCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataTable RetrieveData(this SqlCommand command, string sql) { return command.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataTable RetrieveData(this SqlCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveData(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this SqlCommand command, string sql, params object[] parameters) { return command.RetrieveData(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataTable RetrieveData(this SqlCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, parameters).Tables[0]; } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataTable RetrieveData(this IDbCommand command, Type dataAdapterType, string sql, params object[] parameters) { return command.RetrieveData(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the first /// of result set, if the result set contains multiple tables. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataTable RetrieveData(this IDbCommand command, Type dataAdapterType, string sql, int timeout, params object[] parameters) { return command.RetrieveDataSet(dataAdapterType, sql, timeout, parameters).Tables[0]; } #endregion #region [ RetrieveDataSet Overloaded Extensions ] /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this OleDbConnection connection, string sql) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this OleDbConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OleDbConnection connection, string sql, params object[] parameters) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static DataSet RetrieveDataSet(this OleDbConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { command.PopulateParameters(parameters); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this OdbcConnection connection, string sql) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this OdbcConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OdbcConnection connection, string sql, params object[] parameters) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static DataSet RetrieveDataSet(this OdbcConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { using (OdbcCommand command = new OdbcCommand(sql, connection)) { command.PopulateParameters(parameters); OdbcDataAdapter dataAdapter = new OdbcDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } } /// /// Executes the SQL statement using , and returns the that /// may contain multiple table depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this SqlConnection connection, string sql) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this SqlConnection connection, string sql, int startRow, int maxRows, int timeout) { return connection.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this SqlConnection connection, string sql, params object[] parameters) { return connection.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static DataSet RetrieveDataSet(this SqlConnection connection, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandTimeout = timeout; command.PopulateParameters(parameters); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataSet RetrieveDataSet(this IDbConnection connection, Type dataAdapterType, string sql, params object[] parameters) { return connection.RetrieveDataSet(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataSet RetrieveDataSet(this IDbConnection connection, Type dataAdapterType, string sql, int timeout, params object[] parameters) { using (IDbCommand command = connection.CreateParameterizedCommand(sql, parameters)) { command.CommandTimeout = timeout; IDataAdapter dataAdapter = (IDataAdapter)Activator.CreateInstance(dataAdapterType, command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data); return data; } } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this OleDbCommand command, string sql) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this OleDbCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OleDbCommand command, string sql, params object[] parameters) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OleDbCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { command.Parameters.Clear(); command.PopulateParameters(parameters); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this OdbcCommand command, string sql) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this OdbcCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OdbcCommand command, string sql, params object[] parameters) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this OdbcCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { command.Parameters.Clear(); command.PopulateParameters(parameters); OdbcDataAdapter dataAdapter = new OdbcDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } /// /// Executes the SQL statement using , and returns the that /// may contain multiple table depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// A object. public static DataSet RetrieveDataSet(this SqlCommand command, string sql) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// A object. public static DataSet RetrieveDataSet(this SqlCommand command, string sql, int startRow, int maxRows, int timeout) { return command.RetrieveDataSet(sql, startRow, maxRows, timeout, null); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this SqlCommand command, string sql, params object[] parameters) { return command.RetrieveDataSet(sql, 0, int.MaxValue, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The SQL statement to be executed. /// The to use for executing the SQL statement. /// The zero-based record number to start with. /// The maximum number of records to retrieve. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. /// A object. public static DataSet RetrieveDataSet(this SqlCommand command, string sql, int startRow, int maxRows, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.PopulateParameters(parameters); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data, startRow, maxRows, "Table1"); return data; } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataSet RetrieveDataSet(this IDbCommand command, Type dataAdapterType, string sql, params object[] parameters) { return command.RetrieveDataSet(dataAdapterType, sql, DefaultTimeoutDuration, parameters); } /// /// Executes the SQL statement using , and returns the that /// may contain multiple tables, depending on the SQL statement. /// /// The to use for executing the SQL statement. /// The of data adapter to use to retreieve data. /// The SQL statement to be executed. /// The time in seconds to wait for the SQL statement to execute. /// The parameter values to be used to fill in parameters identified by '@' prefix in expression. /// A object. public static DataSet RetrieveDataSet(this IDbCommand command, Type dataAdapterType, string sql, int timeout, params object[] parameters) { command.CommandTimeout = timeout; command.Parameters.Clear(); command.AddParametersWithValues(sql, parameters); IDataAdapter dataAdapter = (IDataAdapter)Activator.CreateInstance(dataAdapterType, command); DataSet data = new DataSet("Temp"); dataAdapter.Fill(data); return data; } #endregion #region [ DataRow Extensions ] /// /// Provides strongly-typed access to each of the column values in the specified row. /// Automatically applies type conversion to the column values. /// /// A generic parameter that specifies the return type of the column. /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// The value, of type T, of the specified by . public static T ConvertField(this DataRow row, string field) { return ConvertField(row, field, default(T)); } /// /// Provides strongly-typed access to each of the column values in the specified row. /// Automatically applies type conversion to the column values. /// /// A generic parameter that specifies the return type of the column. /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// The value to be substituted if is retrieved. /// The value, of type T, of the specified by . public static T ConvertField(this DataRow row, string field, T defaultValue) { object value = row.Field(field); if (value == null || value == DBNull.Value) return defaultValue; // If the value is an instance of the given type, // no type conversion is necessary if (value is T typeValue) return typeValue; Type type = typeof(T); // Nullable types cannot be used in type conversion, but we can use Nullable.GetUnderlyingType() // to determine whether the type is nullable and convert to the underlying type instead Type underlyingType = Nullable.GetUnderlyingType(type) ?? type; // Handle Guids as a special case since they do not implement IConvertible if (underlyingType == typeof(Guid)) return (T)(object)Guid.Parse(value.ToString()); // Handle enums as a special case since they do not implement IConvertible if (underlyingType.IsEnum) return (T)Enum.Parse(underlyingType, value.ToString()); return (T)Convert.ChangeType(value, underlyingType); } /// /// Automatically applies type conversion to column values when only a type is available. /// /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// Type of the column. /// The value of the specified by . public static object ConvertField(this DataRow row, string field, Type type) { return ConvertField(row, field, type, null); } /// /// Automatically applies type conversion to column values when only a type is available. /// /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// Type of the column. /// The value to be substituted if is retrieved. /// The value of the specified by . public static object ConvertField(this DataRow row, string field, Type type, object defaultValue) { object value = row.Field(field); if (value == null || value == DBNull.Value) return defaultValue ?? (type.IsValueType ? Activator.CreateInstance(type) : null); // If the value is an instance of the given type, // no type conversion is necessary if (type.IsInstanceOfType(value)) return value; // Nullable types cannot be used in type conversion, but we can use Nullable.GetUnderlyingType() // to determine whether the type is nullable and convert to the underlying type instead Type underlyingType = Nullable.GetUnderlyingType(type) ?? type; // Handle Guids as a special case since they do not implement IConvertible if (underlyingType == typeof(Guid)) return Guid.Parse(value.ToString()); // Handle enums as a special case since they do not implement IConvertible if (underlyingType.IsEnum) return Enum.Parse(underlyingType, value.ToString()); return Convert.ChangeType(value, underlyingType); } /// /// Provides strongly-typed access to each of the column values in the specified row. /// Automatically applies type conversion to the column values. /// /// A generic parameter that specifies the return type of the column. /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// The value, of type T, of the specified by . public static T? ConvertNullableField(this DataRow row, string field) where T : struct { object value = row.Field(field); if (value == null) return null; return (T)Convert.ChangeType(value, typeof(T)); } /// /// Parses a Guid from a database field that is a Guid type or a string representing a Guid. /// /// The input , which acts as the this instance for the extension method. /// The name of the column to return the value of. /// The value to be substituted if is retrieved; defaults to . /// The value of the specified by . public static Guid ConvertGuidField(this DataRow row, string field, Guid? defaultValue = null) { object value = row.Field(field); if (value == null || value == DBNull.Value) return defaultValue ?? Guid.Empty; if (value is Guid guid) return guid; return Guid.Parse(value.ToString()); } #endregion #region [ UpdateData Overloaded Functions ] /// /// Updates the underlying data of the using , and /// returns the number of rows successfully updated. /// /// The used to update the underlying data source. /// The SQL statement used initially to populate the . /// The to use for updating the underlying data source. /// The number of rows successfully updated from the . [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "commandBuilder"), SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults"), SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int UpdateData(this OleDbConnection connection, DataTable sourceData, string sourceSql) { OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sourceSql, connection); OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter); return dataAdapter.Update(sourceData); } /// /// Updates the underlying data of the using , and /// returns the number of rows successfully updated. /// /// The used to update the underlying data source. /// The SQL statement used initially to populate the . /// The to use for updating the underlying data source. /// The number of rows successfully updated from the . [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "commandBuilder"), SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults"), SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int UpdateData(this OdbcConnection connection, DataTable sourceData, string sourceSql) { OdbcDataAdapter dataAdapter = new OdbcDataAdapter(sourceSql, connection); OdbcCommandBuilder commandBuilder = new OdbcCommandBuilder(dataAdapter); return dataAdapter.Update(sourceData); } /// /// Updates the underlying data of the using , and /// returns the number of rows successfully updated. /// /// The used to update the underlying data source. /// The SQL statement used initially to populate the . /// The to use for updating the underlying data source. /// The number of rows successfully updated from the . [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "commandBuilder"), SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults"), SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static int UpdateData(this SqlConnection connection, DataTable sourceData, string sourceSql) { SqlDataAdapter dataAdapter = new SqlDataAdapter(sourceSql, connection); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); return dataAdapter.Update(sourceData); } #endregion #region [ Command Parameter Population Functions ] /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The first parameter value to populate the parameters with. /// The remaining parameter values to populate the parameters with. public static void PopulateParameters(this OleDbCommand command, object parameter1, params object[] parameters) { command.PopulateParameters((new[] { parameter1 }).Concat(parameters).ToArray()); } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The parameter values to populate the parameters with. /// /// Automatic parameter derivation is currently not support for OleDB connections under Mono deployments. /// public static void PopulateParameters(this OleDbCommand command, object[] parameters) { #if !MONO command.PopulateParameters(OleDbCommandBuilder.DeriveParameters, parameters); #endif } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The first parameter value to populate the parameters with. /// The remaining parameter values to populate the parameters with. public static void PopulateParameters(this OdbcCommand command, object parameter1, params object[] parameters) { command.PopulateParameters((new[] { parameter1 }).Concat(parameters).ToArray()); } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The parameter values to populate the parameters with. /// /// Automatic parameter derivation is currently not support for ODBC connections under Mono deployments. /// public static void PopulateParameters(this OdbcCommand command, object[] parameters) { #if !MONO command.PopulateParameters(OdbcCommandBuilder.DeriveParameters, parameters); #endif } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The first parameter value to populate the parameters with. /// The remaining parameter values to populate the parameters with. public static void PopulateParameters(this SqlCommand command, object parameter1, params object[] parameters) { command.PopulateParameters((new[] { parameter1 }).Concat(parameters).ToArray()); } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The parameter values to populate the parameters with. public static void PopulateParameters(this SqlCommand command, object[] parameters) { command.PopulateParameters(SqlCommandBuilder.DeriveParameters, parameters); } /// /// Takes the object and populates it with the given parameters. /// /// The whose parameters are to be populated. /// The DeriveParameters() implementation of the to use to populate parameters. /// The parameter values to populate the parameters with. /// Then type to be used. /// /// Number of arguments in of this , identified by '@', do not match number of supplied parameter -or- /// You have supplied more than parameters listed for the stored procedure. /// public static void PopulateParameters(this TDbCommand command, Action deriveParameters, object[] values) where TDbCommand : IDbCommand { // tmshults 12/10/2004 if ((object)values != null) { string commandText = command.CommandText; if (string.IsNullOrEmpty(commandText)) throw new ArgumentNullException(nameof(command), "command.CommandText is null"); // Add parameters for standard SQL expressions (i.e., non stored procedure expressions) if (!IsStoredProcedure(commandText)) { command.AddParametersWithValues(commandText, values); return; } command.CommandType = CommandType.StoredProcedure; // Makes quick query to db to find the parameters for the StoredProc, and then creates them for // the command. The DeriveParameters() is only for commands with CommandType of StoredProcedure. deriveParameters(command); // Removes the ReturnValue Parameter. command.Parameters.RemoveAt(0); // Checks to see if the Parameters found match the Values provided. if (command.Parameters.Count != values.Length) { // If there are more values than parameters, throws an error. if (values.Length > command.Parameters.Count) throw new ArgumentException("You have supplied more values than parameters listed for the stored procedure"); // Otherwise, assume that the missing values are for Parameters that have default values, // and the code uses the default. To do this fill the extended ParamValue as Nothing/Null. Array.Resize(ref values, command.Parameters.Count); // Makes the Values array match the Parameters of the Stored Proc. } // Assigns the values to the the Parameters. for (int i = 0; i < command.Parameters.Count; i++) { ((DbParameter)command.Parameters[i]).Value = values[i]; } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsStoredProcedure(string sql) { // No lock required for this use case: https://docs.microsoft.com/en-us/dotnet/standard/base-types/thread-safety-in-regular-expressions return s_sqlIdentifierRegex.IsMatch(sql.Trim()); } /// /// Creates and adds an to the object with the specified . /// /// to which parameter needs to be added. /// Name of the to be added. /// Value of the to be added. /// for . public static void AddParameterWithValue(this IDbCommand command, string name, object value, ParameterDirection direction = ParameterDirection.Input) { if (value is IDbDataParameter) { // Value is already a parameter. command.Parameters.Add(value); } else { // Create a parameter for the value. IDbDataParameter parameter = command.CreateParameter(); parameter.ParameterName = name; parameter.Value = value; parameter.Direction = direction; command.Parameters.Add(parameter); } } /// /// Creates and adds a new for each of the specified to the object. /// /// to which parameters need to be added. /// The SQL statement. /// The values for the parameters of the in the order that they appear in the SQL statement. /// /// /// This method does very rudimentary parsing of the SQL statement so parameter names should start with the '@' /// character and should be surrounded by either spaces, parentheses, or commas. /// /// /// Do not use the same parameter name twice in the expression so that each parameter, identified by '@', will /// have a corresponding value. /// /// /// The fully populated parameterized command. /// Number of arguments in expression, identified by '@', do not match number of supplied parameter . [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")] public static void AddParametersWithValues(this IDbCommand command, string sql, params object[] values) { if (values.FirstOrDefault(value => value is IDbDataParameter) != null) { // Values are already parameters. foreach (object param in values) { command.Parameters.Add(param); } } else { // Pick up all parameters that start with @ or : but skip key words such as @@IDENTITY string[] tokens = sql.Split(' ', '(', ')', ',', '=') .Where(token => token.StartsWith(":", StringComparison.Ordinal) || token.StartsWith("@", StringComparison.Ordinal) && !token.StartsWith("@@", StringComparison.Ordinal)) .Distinct() .Where(IsValidParameter) .ToArray(); int i = 0; if (tokens.Length != values.Length) throw new ArgumentException("Number of parameter arguments in sql expression do not match number of supplied values", nameof(values)); foreach (string token in tokens) { if (!command.Parameters.Contains(token)) command.AddParameterWithValue(token, values[i++]); } } command.CommandText = sql; } private static bool IsValidParameter(string token) { // No lock required for this use case: https://docs.microsoft.com/en-us/dotnet/standard/base-types/thread-safety-in-regular-expressions return s_sqlParameterRegex.IsMatch(token); } /// /// Creates and returns a parameterized . Parameter names are embedded in the SQL statement /// passed as a parameter to this method. /// /// The database connection. /// The SQL statement. /// The values for the parameters of the in the order that they appear in the SQL statement. /// /// /// This method does very rudimentary parsing of the SQL statement so parameter names should start with the '@' /// character and should be surrounded by either spaces, parentheses, or commas. /// /// /// Do not use the same parameter name twice in the expression so that each parameter, identified by '@', will /// have a corresponding value. /// /// /// The fully populated parameterized command. /// Number of arguments in expression, identified by '@', do not match number of supplied parameter . public static IDbCommand CreateParameterizedCommand(this IDbConnection connection, string sql, params object[] values) { IDbCommand command = connection.CreateCommand(); command.AddParametersWithValues(sql, values); if (IsStoredProcedure(sql)) { command.CommandType = CommandType.StoredProcedure; // Force parameters for stored procedures to have no name - cannot determine proper // name in a database abstract way. As a result, callers must specify proper number // of parameters for stored procedure, in order. foreach (IDbDataParameter parameter in command.Parameters) parameter.ParameterName = null; } return command; } #endregion #region [ CSV / DataTable Conversion Functions ] /// /// Converts a delimited string into a . /// /// The delimited text to be converted to . /// The character(s) used for delimiting the text. /// true, if the delimited text contains header information; otherwise, false. /// A object. public static DataTable ToDataTable(this string delimitedData, string delimiter, bool header) { DataTable table = new DataTable(); string pattern; // Regex pattern that will be used to split the delimited data. pattern = Regex.Escape(delimiter) + "(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"; // Remove any leading and trailing whitespaces, carriage returns or line feeds. delimitedData = delimitedData.Trim().Trim('\r', '\n').Replace("\n", ""); string[] lines = delimitedData.Split('\r'); //Splits delimited data into lines. int cursor = 0; // Assumes that the first line has header information. string[] headers = Regex.Split(lines[cursor], pattern); // Creates columns. if (header) { // Uses the first row as header row. for (int i = 0; i < headers.Length; i++) { table.Columns.Add(new DataColumn(headers[i].Trim('\"'))); //Remove any leading and trailing quotes from the column name. } cursor++; } else { for (int i = 0; i < headers.Length; i++) { table.Columns.Add(new DataColumn()); } } // Populates the data table with csv data. for (; cursor < lines.Length; cursor++) { // Creates new row. DataRow row = table.NewRow(); // Populates the new row. string[] fields = Regex.Split(lines[cursor], pattern); for (int i = 0; i < fields.Length; i++) { // Removes any leading and trailing quotes from the data. row[i] = fields[i].Trim('\"'); } // Adds the new row. table.Rows.Add(row); } return table; } /// /// Converts the to a multi-line delimited string (e.g., CSV export). /// /// The whose data is to be converted to delimited text. /// The character(s) to be used for delimiting the text. /// true, if text is to be surrounded by quotes; otherwise, false. /// true, if the delimited text should have header information. /// A string of delimited text. public static string ToDelimitedString(this DataTable table, string delimiter, bool quoted, bool header) { StringBuilder data = new StringBuilder(); //Uses the column names as the headers if headers are requested. if (header) { for (int i = 0; i < table.Columns.Count; i++) { data.Append((quoted ? "\"" : "") + table.Columns[i].ColumnName + (quoted ? "\"" : "")); if (i < table.Columns.Count - 1) { data.Append(delimiter); } } data.Append("\r\n"); } for (int i = 0; i < table.Rows.Count; i++) { //Converts data table's data to delimited data. for (int j = 0; j < table.Columns.Count; j++) { data.Append((quoted ? "\"" : "") + table.Rows[i][j] + (quoted ? "\"" : "")); if (j < table.Columns.Count - 1) { data.Append(delimiter); } } data.Append("\r\n"); } //Returns the delimited data. return data.ToString(); } #endregion #region [ Oracle Extensions ] // Because of reference dependency, these should be added to a GSF.Data assembly along with MySql versions if useful ///// ///// Executes the SQL statement using , and returns the number of rows affected. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// The number of rows affected. //public static int ExecuteNonQuery(this OracleConnection connection, string sql, params object[] parameters) //{ // OracleCommand command = new OracleCommand(sql, connection); // command.PopulateParameters(parameters); // return command.ExecuteNonQuery(); //} ///// ///// Executes the SQL statement using , and builds a . ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static OracleDataReader ExecuteReader(this OracleConnection connection, string sql, params object[] parameters) //{ // return connection.ExecuteReader(sql, CommandBehavior.Default, parameters); //} ///// ///// Executes the SQL statement using , and builds a . ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// One of the values. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static OracleDataReader ExecuteReader(this OracleConnection connection, string sql, CommandBehavior behavior, params object[] parameters) //{ // OracleCommand command = new OracleCommand(sql, connection); // command.PopulateParameters(parameters); // return command.ExecuteReader(behavior); //} ///// ///// Executes the SQL statement using , and returns the value in the first column ///// of the first row in the result set. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// Value in the first column of the first row in the result set. //public static object ExecuteScalar(this OracleConnection connection, string sql, params object[] parameters) //{ // OracleCommand command = new OracleCommand(sql, connection); // command.PopulateParameters(parameters); // return command.ExecuteScalar(); //} ///// ///// Executes the SQL statement using , and returns the first in the result set. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The first in the result set. //public static DataRow RetrieveRow(this OracleConnection connection, string sql) //{ // return connection.RetrieveRow(sql, null); //} ///// ///// Executes the SQL statement using , and returns the first in the result set. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// The first in the result set. //public static DataRow RetrieveRow(this OracleConnection connection, string sql, params object[] parameters) //{ // DataTable dataTable = connection.RetrieveData(sql, 0, 1, parameters); // if (dataTable.Rows.Count == 0) // dataTable.Rows.Add(dataTable.NewRow()); // return dataTable.Rows[0]; //} ///// ///// Executes the SQL statement using , and returns the first ///// of result set, if the result set contains multiple tables. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// A object. //public static DataTable RetrieveData(this OracleConnection connection, string sql) //{ // return connection.RetrieveData(sql, 0, int.MaxValue); //} ///// ///// Executes the SQL statement using , and returns the first ///// of result set, if the result set contains multiple tables. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The zero-based record number to start with. ///// The maximum number of records to retrieve. ///// A object. //public static DataTable RetrieveData(this OracleConnection connection, string sql, int startRow, int maxRows) //{ // return connection.RetrieveData(sql, startRow, maxRows, null); //} ///// ///// Executes the SQL statement using , and returns the first ///// of result set, if the result set contains multiple tables. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static DataTable RetrieveData(this OracleConnection connection, string sql, params object[] parameters) //{ // return connection.RetrieveData(sql, 0, int.MaxValue, parameters); //} ///// ///// Executes the SQL statement using , and returns the first ///// of result set, if the result set contains multiple tables. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The zero-based record number to start with. ///// The maximum number of records to retrieve. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static DataTable RetrieveData(this OracleConnection connection, string sql, int startRow, int maxRows, params object[] parameters) //{ // return connection.RetrieveDataSet(sql, startRow, maxRows, parameters).Tables[0]; //} ///// ///// Executes the SQL statement using , and returns the that ///// may contain multiple tables, depending on the SQL statement. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// A object. //public static DataSet RetrieveDataSet(this OracleConnection connection, string sql) //{ // return connection.RetrieveDataSet(sql, 0, int.MaxValue); //} ///// ///// Executes the SQL statement using , and returns the that ///// may contain multiple tables, depending on the SQL statement. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The zero-based record number to start with. ///// The maximum number of records to retrieve. ///// A object. //public static DataSet RetrieveDataSet(this OracleConnection connection, string sql, int startRow, int maxRows) //{ // return connection.RetrieveDataSet(sql, startRow, maxRows, null); //} ///// ///// Executes the SQL statement using , and returns the that ///// may contain multiple tables, depending on the SQL statement. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static DataSet RetrieveDataSet(this OracleConnection connection, string sql, params object[] parameters) //{ // return connection.RetrieveDataSet(sql, 0, int.MaxValue, parameters); //} ///// ///// Executes the SQL statement using , and returns the that ///// may contain multiple tables, depending on the SQL statement. ///// ///// The SQL statement to be executed. ///// The to use for executing the SQL statement. ///// The zero-based record number to start with. ///// The maximum number of records to retrieve. ///// The parameter values to be used to fill in parameters identified by '@' prefix in expression -or- the parameter values to be passed into stored procedure being executed. ///// A object. //public static DataSet RetrieveDataSet(this OracleConnection connection, string sql, int startRow, int maxRows, params object[] parameters) //{ // OracleCommand command = new OracleCommand(sql, connection); // command.PopulateParameters(parameters); // OracleDataAdapter dataAdapter = new OracleDataAdapter(command); // DataSet data = new DataSet("Temp"); // dataAdapter.Fill(data, startRow, maxRows, "Table1"); // return data; //} ///// ///// Updates the underlying data of the using , and ///// returns the number of rows successfully updated. ///// ///// The used to update the underlying data source. ///// The SQL statement used initially to populate the . ///// The to use for updating the underlying data source. ///// The number of rows successfully updated from the . //public static int UpdateData(this OracleConnection connection, DataTable sourceData, string sourceSql) //{ // OracleDataAdapter dataAdapter = new OracleDataAdapter(sourceSql, connection); // OracleCommandBuilder commandBuilder = new OracleCommandBuilder(dataAdapter); // return dataAdapter.Update(sourceData); //} ///// ///// Takes the object and populates it with the given parameters. ///// ///// The whose parameters are to be populated. ///// The parameter values to populate the parameters with. //public static void PopulateParameters(this OracleCommand command, object[] parameters) //{ // command.PopulateParameters(OracleCommandBuilder.DeriveParameters, parameters); //} #endregion } }