|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ADODataService
{
public class DataAccess : IDisposable
{
string _connectionString;
SqlConnection _connection;
SqlTransaction _transaction;
/// <summary>
/// Constructor
/// </summary>
public DataAccess()
{
_connectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnection"]);
_connection = new SqlConnection();
_connection.ConnectionString = _connectionString;
_connection.Open();
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
CloseConnection();
}
/// <summary>
/// Close Connection
/// </summary>
private void CloseConnection()
{
if (_transaction != null)
_transaction.Dispose();
_connection.Close();
_connection.Dispose();
}
/// <summary>
/// Execute Data Reader
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public SqlDataReader ExecuteReader(SqlCommand sqlCommand)
{
sqlCommand.Connection = _connection;
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
return sqlDataReader;
}
/// <summary>
/// Execute Non Query
/// </summary>
/// <param name="sqlCommand"></param>
public void ExecuteNonQuery(SqlCommand sqlCommand)
{
sqlCommand.Connection = _connection;
if (_transaction != null)
sqlCommand.Transaction = _transaction;
sqlCommand.ExecuteNonQuery();
}
/// <summary>
/// Execute Non Query
/// </summary>
/// <param name="sqlCommand"></param>
public int ExecuteScalar(SqlCommand sqlCommand)
{
sqlCommand.Connection = _connection;
if (_transaction != null)
sqlCommand.Transaction = _transaction;
int returnValue = int.Parse(sqlCommand.ExecuteScalar().ToString());
return returnValue;
}
/// <summary>
/// Begin Transaction
/// </summary>
public void BeginTransaction()
{
_transaction = _connection.BeginTransaction();
}
/// <summary>
/// Commit Transaction
/// </summary>
public void CommitTransaction()
{
_transaction.Commit();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.
For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.
When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.
In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.
You can contact Mark at mark.caplin@gmail.com
...