SaaS CMS has officially launched! Learn more now.

Class SqlHelper

The SqlHelper class is intended to encapsulate high performance, scalable best practices for common uses of SqlClient.

Inheritance
System.Object
SqlHelper
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Mediachase.BusinessFoundation.Data.Sql
Assembly: Mediachase.BusinessFoundation.Data.dll
Version: 11.8.3
Syntax
public static class SqlHelper

Methods

CreateRetvalSqlParameter()

Creates the retval SQL parameter.

Declaration
public static SqlParameter CreateRetvalSqlParameter()
Returns
Type Description
System.Data.SqlClient.SqlParameter

DBNull2Null(Object)

Converts DBNull into null.

Declaration
public static object DBNull2Null(object Value)
Parameters
Type Name Description
System.Object Value

The value.

Returns
Type Description
System.Object

DBNull2Null(Object, Object)

Converts DBNull into null.

Declaration
public static object DBNull2Null(object Value, object DefaultValue)
Parameters
Type Name Description
System.Object Value

The value.

System.Object DefaultValue

The default value.

Returns
Type Description
System.Object

Deserialize(Byte[])

Deserializes the specified data.

Declaration
public static object Deserialize(byte[] Data)
Parameters
Type Name Description
System.Byte[] Data

The data.

Returns
Type Description
System.Object

ExecuteDataset(SqlContext, CommandType, String)

Executes the dataset.

Declaration
public static DataSet ExecuteDataset(SqlContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

Returns
Type Description
System.Data.DataSet

ExecuteDataset(SqlContext, CommandType, String, SqlParameter[])

Executes the dataset.

Declaration
public static DataSet ExecuteDataset(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

System.Data.SqlClient.SqlParameter[] commandParameters

The command parameters.

Returns
Type Description
System.Data.DataSet

ExecuteDataset(SqlConnection, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.

Declaration
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");

ExecuteDataset(SqlConnection, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameters.

Declaration
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteDataset(SqlConnection, SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static DataSet ExecuteDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteDataset(SqlConnection, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);

ExecuteDataset(SqlTransaction, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.

Declaration
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");

ExecuteDataset(SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteDataset(SqlTransaction, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);

ExecuteDataset(String, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in the connection string.

Declaration
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");

ExecuteDataset(String, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the database specified in the connection string using the provided parameters.

Declaration
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

e.g.:
DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteDataset(String, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in the connection string using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.DataSet

a dataset containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);

ExecuteNonQuery(SqlContext, CommandType, String)

Executes the non query.

Declaration
public static int ExecuteNonQuery(SqlContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

Returns
Type Description
System.Int32

ExecuteNonQuery(SqlContext, CommandType, String, SqlParameter[])

Executes the non query.

Declaration
public static int ExecuteNonQuery(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

System.Data.SqlClient.SqlParameter[] commandParameters

The command parameters.

Returns
Type Description
System.Int32

ExecuteNonQuery(SqlConnection, CommandType, String)

Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection.

Declaration
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");

ExecuteNonQuery(SqlConnection, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns no resultset) against the specified SqlConnection using the provided parameters.

Declaration
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

ExecuteNonQuery(SqlConnection, SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static int ExecuteNonQuery(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteNonQuery(SqlConnection, SqlTransaction, CommandType, String, Int32, SqlParameter[])

Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static int ExecuteNonQuery(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, int commandTimeOut, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection.

System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction.

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command.

System.Int32 commandTimeOut

the command time out.

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command.

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command.

Remarks

e.g.:
int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, 40, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteNonQuery(SqlConnection, String, Object[])

Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlConnection using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);

ExecuteNonQuery(SqlTransaction, CommandType, String)

Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlTransaction.

Declaration
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");

ExecuteNonQuery(SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteNonQuery(SqlTransaction, String, Object[])

Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);

ExecuteNonQuery(String, CommandType, String)

Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in the connection string.

Declaration
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");

ExecuteNonQuery(String, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns no resultset) against the database specified in the connection string using the provided parameters.

Declaration
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

e.g.:
int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

ExecuteNonQuery(String, String, Object[])

Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in the connection string using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.String spName

the name of the stored prcedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Int32

an int representing the number of rows affected by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);

ExecuteReader(SqlContext, CommandType, String)

Executes the reader.

Declaration
public static SqlDataReader ExecuteReader(SqlContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

Returns
Type Description
System.Data.SqlClient.SqlDataReader

ExecuteReader(SqlContext, CommandType, String, SqlParameter[])

Executes the reader.

Declaration
public static SqlDataReader ExecuteReader(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
SqlContext context

The context.

System.Data.CommandType commandType

Type of the command.

System.String commandText

The command text.

System.Data.SqlClient.SqlParameter[] commandParameters

The command parameters.

Returns
Type Description
System.Data.SqlClient.SqlDataReader

ExecuteReader(SqlConnection, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.

Declaration
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");

ExecuteReader(SqlConnection, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameters.

Declaration
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteReader(SqlConnection, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);

ExecuteReader(SqlTransaction, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.

Declaration
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");

ExecuteReader(SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteReader(SqlTransaction, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);

ExecuteReader(String, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in the connection string.

Declaration
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");

ExecuteReader(String, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the database specified in the connection string using the provided parameters.

Declaration
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

e.g.:
SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteReader(String, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in the connection string using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Data.SqlClient.SqlDataReader

a SqlDataReader containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);

ExecuteScalar(SqlContext, CommandType, String, SqlParameter[])

Declaration
public static object ExecuteScalar(SqlContext context, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
SqlContext context
System.Data.CommandType commandType
System.String commandText
System.Data.SqlClient.SqlParameter[] commandParameters
Returns
Type Description
System.Object

ExecuteScalar(SqlConnection, CommandType, String)

Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlConnection.

Declaration
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");

ExecuteScalar(SqlConnection, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection using the provided parameters.

Declaration
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));

ExecuteScalar(SqlConnection, SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static object ExecuteScalar(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));

ExecuteScalar(SqlConnection, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);

ExecuteScalar(SqlTransaction, CommandType, String)

Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlTransaction.

Declaration
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");

ExecuteScalar(SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));

ExecuteScalar(SqlTransaction, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);

ExecuteScalar(String, CommandType, String)

Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in the connection string.

Declaration
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");

ExecuteScalar(String, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a 1x1 resultset) against the database specified in the connection string using the provided parameters.

Declaration
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

e.g.:
int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));

ExecuteScalar(String, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the database specified in the connection string using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.String connectionString

a valid connection string for a SqlConnection

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Object

an object containing the value in the 1x1 resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);

ExecuteScript(SqlContext, String, SqlParameter[])

Executes the sql script.

Declaration
public static void ExecuteScript(SqlContext context, string Script, params SqlParameter[] parameters)
Parameters
Type Name Description
SqlContext context

The context.

System.String Script

The script.

System.Data.SqlClient.SqlParameter[] parameters

The parameters.

ExecuteXmlReader(SqlConnection, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.

Declaration
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command using "FOR XML AUTO"

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

e.g.:
XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");

ExecuteXmlReader(SqlConnection, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameters.

Declaration
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command using "FOR XML AUTO"

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

e.g.:
XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteXmlReader(SqlConnection, SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static XmlReader ExecuteXmlReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command using "FOR XML AUTO"

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

e.g.:
XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteXmlReader(SqlConnection, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection

a valid SqlConnection

System.String spName

the name of the stored procedure using "FOR XML AUTO"

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);

ExecuteXmlReader(SqlTransaction, CommandType, String)

Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.

Declaration
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command using "FOR XML AUTO"

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

e.g.:
XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");

ExecuteXmlReader(SqlTransaction, CommandType, String, SqlParameter[])

Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameters.

Declaration
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command using "FOR XML AUTO"

System.Data.SqlClient.SqlParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
System.Xml.XmlReader

an XmlReader containing the resultset generated by the command

Remarks

e.g.:
XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));

ExecuteXmlReader(SqlTransaction, String, Object[])

Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction using the provided parameter values. This method will query the database to discover the parameters for the stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.

Declaration
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues)
Parameters
Type Name Description
System.Data.SqlClient.SqlTransaction transaction

a valid SqlTransaction

System.String spName

the name of the stored procedure

System.Object[] parameterValues

an array of objects to be assigned as the input values of the stored procedure

Returns
Type Description
System.Xml.XmlReader

a dataset containing the resultset generated by the command

Remarks

This method provides no access to output parameters or the stored procedure's return value parameter.

e.g.:
XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);

Null2DBNull(Object)

Converts null into DBNull.

Declaration
public static object Null2DBNull(object Value)
Parameters
Type Name Description
System.Object Value

The value.

Returns
Type Description
System.Object

Null2DBNull(Object, Object)

Declaration
public static object Null2DBNull(object Value, object defaultValue)
Parameters
Type Name Description
System.Object Value
System.Object defaultValue
Returns
Type Description
System.Object

SaveDataSet(SqlContext, DataSet)

Declaration
public static void SaveDataSet(SqlContext context, DataSet ds)
Parameters
Type Name Description
SqlContext context
System.Data.DataSet ds

SaveDataSet(SqlConnection, SqlTransaction, DataSet)

Declaration
public static void SaveDataSet(SqlConnection connection, SqlTransaction tran, DataSet ds)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection
System.Data.SqlClient.SqlTransaction tran
System.Data.DataSet ds

SaveDataSet(String, DataSet)

Declaration
public static void SaveDataSet(string ConnectionString, DataSet ds)
Parameters
Type Name Description
System.String ConnectionString
System.Data.DataSet ds

SaveDataTable(SqlConnection, SqlTransaction, DataTable)

Declaration
public static void SaveDataTable(SqlConnection connection, SqlTransaction tran, DataTable table)
Parameters
Type Name Description
System.Data.SqlClient.SqlConnection connection
System.Data.SqlClient.SqlTransaction tran
System.Data.DataTable table

Serialize(Object)

Serializes the specified value.

Declaration
public static byte[] Serialize(object Value)
Parameters
Type Name Description
System.Object Value

The value.

Returns
Type Description
System.Byte[]

SqlParameter(String, SqlDbType, Int32, ParameterDirection)

Creates SQL parameter.

Declaration
public static SqlParameter SqlParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction)
Parameters
Type Name Description
System.String parameterName

Name of the parameter.

System.Data.SqlDbType dbType

Type of the db.

System.Int32 size

The size.

System.Data.ParameterDirection direction

The direction.

Returns
Type Description
System.Data.SqlClient.SqlParameter

SqlParameter(String, SqlDbType, Int32, Object)

Creates SQL parameter.

Declaration
public static SqlParameter SqlParameter(string parameterName, SqlDbType dbType, int size, object value)
Parameters
Type Name Description
System.String parameterName

Name of the parameter.

System.Data.SqlDbType dbType

Type of the db.

System.Int32 size

The size.

System.Object value

The value.

Returns
Type Description
System.Data.SqlClient.SqlParameter

SqlParameter(String, SqlDbType, Object)

Creates SQL parameter.

Declaration
public static SqlParameter SqlParameter(string parameterName, SqlDbType dbType, object value)
Parameters
Type Name Description
System.String parameterName

Name of the parameter.

System.Data.SqlDbType dbType

Type of the db.

System.Object value

The value.

Returns
Type Description
System.Data.SqlClient.SqlParameter

SqlParameterWithDefault(String, SqlDbType, Object, Object)

Declaration
public static SqlParameter SqlParameterWithDefault(string parameterName, SqlDbType dbType, object value, object defaultValue)
Parameters
Type Name Description
System.String parameterName
System.Data.SqlDbType dbType
System.Object value
System.Object defaultValue
Returns
Type Description
System.Data.SqlClient.SqlParameter

ThrowIfReadonlyMode()

Throws not supported error if read-only mode.

Declaration
public static void ThrowIfReadonlyMode()
Exceptions
Type Condition
System.NotSupportedException