Last active
August 2, 2018 05:22
-
-
Save romeosierra1/7c39dcf1d1ac5faf1db20aaf7de94189 to your computer and use it in GitHub Desktop.
Helper class to perform CRUD operations in ASP.Net Web Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Linq; | |
using System.Web; | |
/// <summary> | |
/// Summary description for CRUDHelper | |
/// </summary> | |
public class DatabaseHelper | |
{ | |
private SqlConnection connection; | |
private SqlDataAdapter adapter; | |
private SqlCommand command; | |
private string Query; | |
public DatabaseHelper() | |
{ | |
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString); | |
} | |
public DatabaseHelper(string ConnectionString) | |
{ | |
connection = new SqlConnection(ConnectionString); | |
} | |
public DataTable SelectQuery(string Query) | |
{ | |
this.Query = Query; | |
command = new SqlCommand(Query, connection); | |
return ExecuteQuery(); | |
} | |
public DataTable SelectAnd(string TableName, Dictionary<string, string> Where = null, List<string> OrderBy = null) | |
{ | |
Query = "SELECT * FROM [" + TableName + "]"; | |
if (Where != null) | |
{ | |
if (Where.Count > 0) | |
{ | |
Query += " WHERE" + GenerateWhere("AND", Where); | |
} | |
} | |
if (OrderBy != null) | |
{ | |
if (OrderBy.Count > 0) | |
{ | |
Query += " ORDER BY" + GenerateOrderBy(OrderBy); | |
} | |
} | |
command = new SqlCommand(Query, connection); | |
BindParameters(Where); | |
return ExecuteQuery(); | |
} | |
public DataTable SelectOr(string TableName, Dictionary<string, string> Where = null, List<string> OrderBy = null) | |
{ | |
Query = "SELECT * FROM [" + TableName + "]"; | |
if (Where != null) | |
{ | |
if (Where.Count > 0) | |
{ | |
Query += " WHERE" + GenerateWhere("AND", Where); | |
} | |
} | |
if (OrderBy != null) | |
{ | |
if (OrderBy.Count > 0) | |
{ | |
Query += " ORDER BY" + GenerateOrderBy(OrderBy); | |
} | |
} | |
command = new SqlCommand(Query, connection); | |
BindParameters(Where); | |
return ExecuteQuery(); | |
} | |
public void Insert(string TableName, Dictionary<string, string> Data) | |
{ | |
Query = "INSERT INTO [" + TableName + "] " + GenerateColumnParameters(false, Data) + " VALUES " + GenerateColumnParameters(true, Data); | |
command = new SqlCommand(Query, connection); | |
BindParameters(Data); | |
ExecuteQuery(); | |
} | |
public void Update(string TableName, Dictionary<string, string> Data, Dictionary<string, string> Where) | |
{ | |
Query = "UPDATE [" + TableName + "] SET " + GenerateUpdateParameters(Data) + " WHERE " + GenerateWhere("AND", Where); | |
command = new SqlCommand(Query, connection); | |
BindParameters(Data); | |
BindParameters(Where); | |
ExecuteQuery(); | |
} | |
public void Delete(string TableName, Dictionary<string, string> Where) | |
{ | |
Query = "DELETE FROM [" + TableName + "] WHERE " + GenerateWhere("AND", Where); | |
command = new SqlCommand(Query, connection); | |
BindParameters(Where); | |
ExecuteQuery(); | |
} | |
private string GenerateWhere(string Operator, Dictionary<string, string> Where) | |
{ | |
string WhereClause = ""; | |
foreach (var item in Where) | |
{ | |
WhereClause += " [" + item.Key + "] = @" + item.Key + " " + Operator; | |
} | |
WhereClause = WhereClause.Substring(0, WhereClause.Length - (Operator.Length + 1)); | |
return WhereClause; | |
} | |
private string GenerateOrderBy(List<string> OrderBy) | |
{ | |
string OrderByClause = ""; | |
foreach (var item in OrderBy) | |
{ | |
OrderByClause += " " + item + ","; | |
} | |
OrderByClause = OrderByClause.Substring(0, OrderByClause.Length - 1); | |
return OrderByClause; | |
} | |
private string GenerateUpdateParameters(Dictionary<string, string> Data) | |
{ | |
string UpdateParameters = ""; | |
foreach (var item in Data) | |
{ | |
UpdateParameters += "[" + item.Key + "] = @" + item.Key + ", "; | |
} | |
UpdateParameters = UpdateParameters.Substring(0, UpdateParameters.Length - 2); | |
return UpdateParameters; | |
} | |
private string GenerateColumnParameters(bool IsParam, Dictionary<string, string> Data) | |
{ | |
string ColumnParameters = "("; | |
foreach (var item in Data) | |
{ | |
if (!IsParam) | |
{ | |
ColumnParameters += "[" + item.Key + "],"; | |
} | |
else | |
{ | |
ColumnParameters += "@" + item.Key + ","; | |
} | |
} | |
ColumnParameters = ColumnParameters.Substring(0, ColumnParameters.Length - 1); | |
ColumnParameters += ")"; | |
return ColumnParameters; | |
} | |
private DataTable ExecuteQuery() | |
{ | |
DataTable table = new DataTable(); | |
adapter = new SqlDataAdapter(command); | |
adapter.Fill(table); | |
return table; | |
} | |
private void BindParameters(Dictionary<string, string> Parameters) | |
{ | |
if (Parameters != null) | |
{ | |
foreach (var Parameter in Parameters) | |
{ | |
command.Parameters.AddWithValue("@" + Parameter.Key, Parameter.Value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment