using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; namespace EPS.Common.Data { /// /// Implementation of IDbhelper for SqlServer databases. /// public class SqlDbHelper : IDbHelper { /// /// Initializes a new instance of the SqlDbHelper class. /// /// public SqlDbHelper(string connectionString) { _connectionString = connectionString; } private string _connectionString; /// /// Gets the connection string to the database. /// public string ConnectionString { get { return _connectionString; } } /// /// Opens a connection to the database. /// /// An open connection to the database. public IDbConnection GetConnection() { return this.GetConnection(InitialConnectionStates.Open); } /// /// Gets a connection to the database. /// /// Indicates the state of the connection returned. /// A connection to the database, either open or closed. public IDbConnection GetConnection(InitialConnectionStates initialState) { IDbConnection result = new SqlConnection(this._connectionString); if (initialState == InitialConnectionStates.Open) result.Open(); return result; } /// /// Executes the query, and returns the first column of the first row of the resultset. /// /// The type of the data returned. /// The query to execute. /// The first column of the first row of the result of executeing the query. public T ExecuteScalar(string commandText) { IDbCommand cmd = new SqlCommand(commandText); return this.ExecuteScalar(cmd); } /// /// Executes the command, and returns the first column of the first row of the resultset. /// /// The type of the data returned. /// The command to execute. /// The first colunm of the first row of the result of executing the command. public T ExecuteScalar(IDbCommand command) { T result; using (SqlConnection cn = (this.GetConnection() as SqlConnection)) { command.Connection = cn; result = (T)command.ExecuteScalar(); } return result; } /// /// Executes the query and returns an array of all of the values of the first column of all rows in the resultset. /// /// The type of data in the first column of each row. /// The query to execute. /// An array of all of the values of the first column of all of the rows in the resultset. public TItem[] ExecuteArray(string commandText) { SqlCommand command = new SqlCommand(commandText); return this.ExecuteArray(command); } /// /// Executes the command and returns an array of all of the values of the first column of all rows in the resultset. /// /// The type of data in the first column of each row. /// The command to execute. /// An array of all of the values of the first column of all of the rows in the resultset. public TItem[] ExecuteArray(IDbCommand command) { List result = new List(); using (IDataReader reader = this.ExecuteReader(command)) { while (reader.Read()) { result.Add((TItem)reader.GetValue(0)); } } return result.ToArray(); } /// /// Executes the query and returns the number of rows affected. /// /// The query to execute. /// The number of rows affected. public int ExecuteNonQuery(string commandText) { SqlCommand command = new SqlCommand(commandText); return this.ExecuteNonQuery(command); } /// /// Executes the command and returns the number of rows affected. /// /// The command to execute. /// The number of rows affected. public int ExecuteNonQuery(IDbCommand command) { int result; using (IDbConnection cn = this.GetConnection()) { command.Connection = cn; result = command.ExecuteNonQuery(); } return result; } /// /// Executes the query and returns a DataTable filled with the results. /// /// The query to execute. /// DataTable containing the results of executing the query. public DataTable ExecuteDataTable(string commandText) { SqlCommand command = new SqlCommand(commandText); return this.ExecuteDataTable(command); } /// /// Executes the command and returns a DataTable filled with the results. /// /// The command to execute. /// DataTable containing the results of executing the command. public DataTable ExecuteDataTable(IDbCommand command) { DataTable result = new DataTable(); using (IDbConnection cn = this.GetConnection()) { command.Connection = cn; using (SqlDataAdapter da = new SqlDataAdapter((SqlCommand)command)) { da.Fill(result); } } return result; } /// /// Executes a query and returns a SqlDataReader containing the resultset. /// /// The query to execute. /// A SqlDataReader containing the resultset. public IDataReader ExecuteReader(string commandText) { SqlCommand command = new SqlCommand(commandText); return this.ExecuteReader(command); } /// /// Executes a command and returns a SqlDataReader containing the resultset. /// /// The command to execute. /// A SqlDataReader containing the resultset. public IDataReader ExecuteReader(IDbCommand command) { command.Connection = this.GetConnection(); return command.ExecuteReader(); } } }