using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DALBlueWhale
{
public class DBWrapper
{
ConnectionProvider _connProvider;
SqlConnection _sqlConnection;
SqlDataAdapter _da;
SqlParameter _sqlparameter;
SqlCommand _command;
DataSet _ds;
private string connStr = "";
/// <summary>
/// Creates the object using connectionString given in config file
/// </summary>
public DBWrapper()
{
_connProvider = new ConnectionProvider();
_sqlConnection = _connProvider.GetDBConnection();
}
/// <summary>
/// create the object using given connection string in parameter
/// </summary>
/// <param name="connstr"></param>
public DBWrapper(string connstr)
{
connStr = connstr;
_connProvider = new ConnectionProvider(connStr);
_sqlConnection = _connProvider.GetDBConnection();
}
public DataSet GetDataSet(string sqlSelectCommandString)
{
_ds = new DataSet();
try
{
_connProvider.ConnectDB();
_da = new SqlDataAdapter(sqlSelectCommandString, _sqlConnection);
_da.Fill(_ds);
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return _ds;
}
/// <summary>
/// Executes a given Insert, update and delete commands
/// </summary>
/// <param name="sqlCommandString">any insert, delete and update command text</param>
/// <returns>no of rows effected</returns>
public int ExicuteQuery(string sqlCommandString)
{
int noRowsEffected = 0;
try
{
_command = new SqlCommand(sqlCommandString, _sqlConnection);
_connProvider.ConnectDB();
noRowsEffected = _command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return noRowsEffected;
}
public int ExcuteSimpleProcedure(string ProcedureName, SqlParameterCollection parameters)
{
int NoRowsAffected = 0;
_command= new SqlCommand();
_command.Connection = _sqlConnection;
_command.CommandType = CommandType.StoredProcedure;
_command.CommandText = "InsertEmployees";
foreach (SqlParameter param in parameters)
{
_command.Parameters.Add(param);
}
try
{
_connProvider.ConnectDB();
NoRowsAffected = _command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return NoRowsAffected;
}
public SqlParameter CreateParameter(string paramName, SqlDbType dbtype, int size, ParameterDirection direction)
{
_sqlparameter = new SqlParameter();
_sqlparameter.ParameterName = paramName;
_sqlparameter.SqlDbType = dbtype;
_sqlparameter.Size = size;
_sqlparameter.Direction = direction;
return _sqlparameter;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DALBlueWhale
{
public class DBWrapper
{
ConnectionProvider _connProvider;
SqlConnection _sqlConnection;
SqlDataAdapter _da;
SqlParameter _sqlparameter;
SqlCommand _command;
DataSet _ds;
private string connStr = "";
/// <summary>
/// Creates the object using connectionString given in config file
/// </summary>
public DBWrapper()
{
_connProvider = new ConnectionProvider();
_sqlConnection = _connProvider.GetDBConnection();
}
/// <summary>
/// create the object using given connection string in parameter
/// </summary>
/// <param name="connstr"></param>
public DBWrapper(string connstr)
{
connStr = connstr;
_connProvider = new ConnectionProvider(connStr);
_sqlConnection = _connProvider.GetDBConnection();
}
public DataSet GetDataSet(string sqlSelectCommandString)
{
_ds = new DataSet();
try
{
_connProvider.ConnectDB();
_da = new SqlDataAdapter(sqlSelectCommandString, _sqlConnection);
_da.Fill(_ds);
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return _ds;
}
/// <summary>
/// Executes a given Insert, update and delete commands
/// </summary>
/// <param name="sqlCommandString">any insert, delete and update command text</param>
/// <returns>no of rows effected</returns>
public int ExicuteQuery(string sqlCommandString)
{
int noRowsEffected = 0;
try
{
_command = new SqlCommand(sqlCommandString, _sqlConnection);
_connProvider.ConnectDB();
noRowsEffected = _command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return noRowsEffected;
}
public int ExcuteSimpleProcedure(string ProcedureName, SqlParameterCollection parameters)
{
int NoRowsAffected = 0;
_command= new SqlCommand();
_command.Connection = _sqlConnection;
_command.CommandType = CommandType.StoredProcedure;
_command.CommandText = "InsertEmployees";
foreach (SqlParameter param in parameters)
{
_command.Parameters.Add(param);
}
try
{
_connProvider.ConnectDB();
NoRowsAffected = _command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
_connProvider.DisconnectDB();
}
return NoRowsAffected;
}
public SqlParameter CreateParameter(string paramName, SqlDbType dbtype, int size, ParameterDirection direction)
{
_sqlparameter = new SqlParameter();
_sqlparameter.ParameterName = paramName;
_sqlparameter.SqlDbType = dbtype;
_sqlparameter.Size = size;
_sqlparameter.Direction = direction;
return _sqlparameter;
}
}
}
0 comments:
Post a Comment