"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Saturday, January 19, 2013

Creating Database Helper class using Extension method


first of pls go through my previous post abt extension method so that u will get a detail view.
 

static public class DatabaseHelper
    {
        static public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["documentDB"].ConnectionString;
            }
        }

        static public DataTable ExecuteQuery(string storedProcedureName, ArrayList parameters)
        {
            SqlConnection connection = new SqlConnection(ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(storedProcedureName, connection);
            foreach (SqlParameter param in parameters)
            {
                adapter.SelectCommand.Parameters.Add(param);
            }

            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            adapter.Fill(ds, "tableData");
            return ds.Tables["tableData"];
        }

        static public object ExecuteScaler(string storedProcedureName, ArrayList parameters)
        {
            SqlConnection connection = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand(storedProcedureName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in parameters)
            {
                command.Parameters.Add(param);
            }
            connection.Open();
            object result=command.ExecuteScalar();
            connection.Close();
            connection.Dispose();
            command.Dispose();
            return result;
        }
        static public SqlParameter CreateIntParameter(string name, int value)
        {
            SqlParameter param = new SqlParameter(name, SqlDbType.Int);
            param.Value = value;
            return param;
        }
        static public SqlParameter CreateStringParameter(string name, string value)
        {
            SqlParameter param = new SqlParameter(name, SqlDbType.NVarChar);
            param.Value = value;
            return param;
        }
        static public SqlParameter CreateDateTimeParameter(string name, DateTime value)
        {
            SqlParameter param = new SqlParameter(name, SqlDbType.DateTime);
            param.Value = value;
            return param;
        }
        static public SqlParameter CreateUniqueIdentifierParameter(string name, Guid value)
        {
            SqlParameter param = new SqlParameter(name, SqlDbType.UniqueIdentifier);
            param.Value = value;
            return param;
        }
    }

 


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: