You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using CustomerOnlineV2.Common.Helper; using CustomerOnlineV2.Common.Models; using Microsoft.Data.SqlClient; using System.Data;
namespace CustomerOnlineV2.Repository.Helper { public class DBHelper { private SqlConnection _connection; public DBHelper() { Init(); }
public void Init() { _connection = new SqlConnection(GetConnectionString()); }
public void OpenConnection() { if (_connection.State == ConnectionState.Open) _connection.Close(); _connection.Open(); }
public void CloseConnection() { if (_connection.State == ConnectionState.Open) this._connection.Close(); }
public string GetConnectionString() { return Common.Helper.Utilities.ReadAppSettings("ConnectionStrings:ApplicationDb"); }
public String FilterString(string strVal) { var str = FilterQuote(strVal);
if (str.ToLower() != "null") str = "'" + str + "'";
return str; }
public String FilterQuote(string strVal) { if (string.IsNullOrEmpty(strVal)) { strVal = ""; } var str = strVal.Trim();
if (!string.IsNullOrEmpty(str)) { str = str.Replace(";", ""); //str = str.Replace(",", "");
str = str.Replace("--", ""); str = str.Replace("'", "");
str = str.Replace("/*", ""); str = str.Replace("*/", "");
str = str.Replace(" select ", ""); str = str.Replace(" insert ", ""); str = str.Replace(" update ", ""); str = str.Replace(" delete ", "");
str = str.Replace(" drop ", ""); str = str.Replace(" truncate ", ""); str = str.Replace(" create ", "");
str = str.Replace(" begin ", ""); str = str.Replace(" end ", ""); str = str.Replace(" char(", "");
str = str.Replace(" exec ", ""); str = str.Replace(" xp_cmd ", "");
str = str.Replace("<script", "");
} else { str = "null"; } return str; }
public DataTable ExecuteDataTable(string sql) { using (var ds = ExecuteDataset(sql)) { if (ds == null || ds.Tables.Count == 0) return null;
return ds.Tables[0]; } } public DataSet ExecuteDataset(string sql) { var ds = new DataSet(); SqlDataAdapter da; try { OpenConnection(); da = new SqlDataAdapter(sql, _connection);
da.Fill(ds); da.Dispose(); CloseConnection(); } catch (Exception ex) { throw ex; } finally { da = null; CloseConnection(); } return ds; }
public DataRow ExecuteDataRow(string sql) { using (var ds = ExecuteDataset(sql)) { if (ds == null || ds.Tables.Count == 0) return null;
if (ds.Tables[0].Rows.Count == 0) return null;
return ds.Tables[0].Rows[0]; } }
public CommonResponse ParseDbResult(string sql) { DataTable dt = ExecuteDataTable(sql); return ParseDbResult(dt); }
public CommonResponse ParseDbResult(DataTable dt) { CommonResponse response = new CommonResponse(101, "Error parsing data!"); if (dt == null || dt.Rows.Count == 0) return response;
response = new CommonResponse(Convert.ToInt16(Utilities.GetRowCellValue(dt.Rows[0][0])), Utilities.GetRowCellValue(dt.Rows[0][1])); if (dt.Columns.Count > 2) response.Id = Utilities.GetRowCellValue(dt.Rows[0][2]); if (dt.Columns.Count > 3) response.Extra = Utilities.GetRowCellValue(dt.Rows[0][3]);
return response; } } }
|