Click here to Skip to main content
15,886,110 members
Articles / Web Development / HTML

MVC Techniques with jQuery, JSON, Knockout, and C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (138 votes)
2 Jan 2012CPOL14 min read 435.5K   22.4K   415  
Developing an Order Entry application with MVC.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Joey Software Solutions
United States United States
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

...

Comments and Discussions