//******************************************************************************************************
// 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 Eclipse Public License -v 1.0 (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/eclipse-1.0.php
//
// 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.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace GSF.Data
{
///
/// 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;
#region [ SQL Encoding String Extension ]
///
/// Performs SQL encoding on given T-SQL string.
///
/// The string on which SQL encoding is to be performed.
/// The SQL encoded string.
public static string SqlEncode(this string sql)
{
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.
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.
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.
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();
}
}
#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.
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.
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.
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);
}
}
#endregion
#region [ ExecuteScalar Overloaded Extensions ]
///
/// Executes the SQL statement using , and returns the value in the first column
/// of the first row in the resultset.
///
/// 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 resultset.
/// 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 resultset.
///
/// 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 resultset.
/// 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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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();
}
}
#endregion
#region [ RetrieveRow Overloaded Extensions ]
///
/// Executes the SQL statement using , and returns the first in the resultset.
///
/// The SQL statement to be executed.
/// The to use for executing the SQL statement.
/// The first in the resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// The SQL statement to be executed.
/// The to use for executing the SQL statement.
/// The first in the resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// The SQL statement to be executed.
/// The to use for executing the SQL statement.
/// The first in the resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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 resultset.
///
/// 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 resultset.
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];
}
#endregion
#region [ RetrieveData Overloaded Extensions ]
///
/// Executes the SQL statement using , and returns the first
/// of resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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 resultset, if the resultset 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];
}
#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.
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.
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.
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;
}
}
#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 columnName.
public static T ConvertField(this DataRow row, string field)
{
object value = row.Field