Class DBHelper

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

Inheritance
System.Object
DBHelper
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.MetaDataPlus.Common
Assembly: Mediachase.MetaDataPlus.dll
Version: 10.8.0
Syntax
public sealed class DBHelper

Methods

DBNull2Null(Object)

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

DBNull2Null(Object, Object)

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

ExecuteDataSet(MetaDataContext, CommandType, String)

Declaration
public static DataResult ExecuteDataSet(MetaDataContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
Returns
Type Description
DataResult

ExecuteDataSet(MetaDataContext, CommandType, String, DataParameter[])

Declaration
public static DataResult ExecuteDataSet(MetaDataContext context, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
DataResult

ExecuteDataSet(String, CommandType, String, DataParameter[])

Declaration
public static DataResult ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
DataResult

ExecuteDataTable(MetaDataContext, CommandType, String)

Declaration
public static DataTable ExecuteDataTable(MetaDataContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
Returns
Type Description
System.Data.DataTable

ExecuteDataTable(MetaDataContext, CommandType, String, DataParameter[])

Declaration
public static DataTable ExecuteDataTable(MetaDataContext context, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
System.Data.DataTable

ExecuteDataTable(MetaDataContext, CommandType, String, Int32, DataParameter[])

Declaration
public static DataTable ExecuteDataTable(MetaDataContext context, CommandType commandType, string commandText, int commandTimeout, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
System.Int32 commandTimeout
DataParameter[] commandParameters
Returns
Type Description
System.Data.DataTable

ExecuteDataTable(String, CommandType, String, DataParameter[])

Declaration
public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
System.Data.DataTable

ExecuteDataTable(String, CommandType, String, Int32, DataParameter[])

Declaration
public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, int commandTimeout, params DataParameter[] commandParameters)
Parameters
Type Name Description
System.String connectionString
System.Data.CommandType commandType
System.String commandText
System.Int32 commandTimeout
DataParameter[] commandParameters
Returns
Type Description
System.Data.DataTable

ExecuteNonQuery(MetaDataContext, CommandType, String)

Declaration
public static DataResult ExecuteNonQuery(MetaDataContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
Returns
Type Description
DataResult

ExecuteNonQuery(MetaDataContext, CommandType, String, DataParameter[])

Declaration
public static DataResult ExecuteNonQuery(MetaDataContext context, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
DataResult

ExecuteNonQuery(MetaDataContext, CommandType, String, Int32, DataParameter[])

Declaration
public static DataResult ExecuteNonQuery(MetaDataContext context, CommandType commandType, string commandText, int commandTimeout, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
System.Int32 commandTimeout
DataParameter[] commandParameters
Returns
Type Description
DataResult

ExecuteNonQuery(String, CommandType, String)

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

Declaration
public static DataResult 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
DataResult

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, DataParameter[])

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

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

The connection string.

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

DataParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
DataResult

an int representing the number of rows affected by the command

Remarks

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

ExecuteNonQuery(String, CommandType, String, Int32, DataParameter[])

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

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

The connection string.

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 wait time before terminating the attempt to execute a command and generating an error.

DataParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
DataResult

an int representing the number of rows affected by the command

Remarks

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

ExecuteReader(MetaDataContext, CommandType, String)

Declaration
public static DataResult ExecuteReader(MetaDataContext context, CommandType commandType, string commandText)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
Returns
Type Description
DataResult

ExecuteReader(MetaDataContext, CommandType, String, DataParameter[])

Declaration
public static DataResult ExecuteReader(MetaDataContext context, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
DataResult

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

Create and prepare a DataCommand, and call ExecuteReader with the appropriate CommandBehavior.

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

The connection string.

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

DataParameter[] commandParameters

an array of DataParameters to be associated with the command or 'null' if no parameters are required

Returns
Type Description
DataResult

IDataReader containing the results of the command

Remarks

If we created and opened the connection, we want the connection to be closed when the DataReader is closed. If the caller provided the connection, we want to leave it to them to manage.

ExecuteScalar(MetaDataContext, CommandType, String, DataParameter[])

Declaration
public static DataResult ExecuteScalar(MetaDataContext context, CommandType commandType, string commandText, params DataParameter[] commandParameters)
Parameters
Type Name Description
MetaDataContext context
System.Data.CommandType commandType
System.String commandText
DataParameter[] commandParameters
Returns
Type Description
DataResult

ExecuteScalar(String, CommandType, String)

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

Declaration
public static DataResult 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
DataResult

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, DataParameter[])

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

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

The connection string.

System.Data.CommandType commandType

the CommandType (stored procedure, text, etc.)

System.String commandText

the stored procedure name or T-SQL command

DataParameter[] commandParameters

an array of SqlParamters used to execute the command

Returns
Type Description
DataResult

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 DataParameter("prodid", 24));

ExecuteScript(MetaDataContext, String)

Declaration
public static void ExecuteScript(MetaDataContext context, string script)
Parameters
Type Name Description
MetaDataContext context
System.String script

GetMetaKey(MetaDataContext, Int32, Int32, Int32)

Declaration
public static int GetMetaKey(MetaDataContext context, int metaObjectId, int metaClassId, int metaFieldId)
Parameters
Type Name Description
MetaDataContext context
System.Int32 metaObjectId
System.Int32 metaClassId
System.Int32 metaFieldId
Returns
Type Description
System.Int32

GetMetaKey(MetaDataContext, Int32, Int32, Int32, String)

Declaration
public static int GetMetaKey(MetaDataContext context, int metaObjectId, int metaClassId, int metaFieldId, string language)
Parameters
Type Name Description
MetaDataContext context
System.Int32 metaObjectId
System.Int32 metaClassId
System.Int32 metaFieldId
System.String language
Returns
Type Description
System.Int32

GetMetaKey(MetaDataContext, Int32, Nullable<Int32>, Int32, Int32, String)

Declaration
public static int GetMetaKey(MetaDataContext context, int metaObjectId, int? workId, int metaClassId, int metaFieldId, string language)
Parameters
Type Name Description
MetaDataContext context
System.Int32 metaObjectId
System.Nullable<System.Int32> workId
System.Int32 metaClassId
System.Int32 metaFieldId
System.String language
Returns
Type Description
System.Int32

Null2DBNull(Object)

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