Wednesday, July 8, 2015

SQLHelper code for database connection

using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace Alert
{
    public abstract class SqlHelper
    {
        public static readonly string ConnectionStringLocalTransaction = TrippleDES.Decrypt(ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString);

        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    if (parm.SqlValue == null) { parm.SqlValue = DBNull.Value; }
                    cmd.Parameters.Add(parm);
                }
            }
        }

        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);

            // we use a try/catch here because if the method throws an exception we want to
            // close the connection throw code, because no datareader will exist, hence the
            // commandBehaviour.CloseConnection will not work
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }

        public static SqlParameter SqlParameter(string paramName, SqlDbType dbType, int Size, object value, bool isDBNullCheck = false, ParameterDirection direction = ParameterDirection.Input)
        {
            System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter(paramName, dbType, Size);
            param.Direction = direction;
            param.Value = value;

            if (isDBNullCheck)
                if (string.IsNullOrEmpty(Convert.ToString(value)))
                {
                    param.Value = DBNull.Value;
                }

            return param;
        }

        public static SqlParameter SqlParameter(string paramName, SqlDbType dbType, object value, bool isDBNullCheck = false, ParameterDirection direction = ParameterDirection.Input)
        {
            System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter(paramName, dbType);
            param.Direction = direction;

            param.Value = value;

            if (isDBNullCheck)
                if (string.IsNullOrEmpty(Convert.ToString(value)))
                {
                    param.Value = DBNull.Value;
                }

            return param;
        }
    }
}

No comments:

Post a Comment