|
|
using JMETxnPushScheduler.Common; using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Text.RegularExpressions;
namespace JMETxnPushScheduler.DAO { public class SwiftDao {
SqlConnection _connection;
public SwiftDao() { Init(); }
private void Init() { _connection = new SqlConnection(GetConnectionString()); }
private string GetConnectionString() { return ConfigurationManager.ConnectionStrings["Connections"].ConnectionString; }
private int GetCommandTimeOut() { int cto = 60; return cto;
}
private void CloseConnection() { if (_connection.State == ConnectionState.Open) this._connection.Close(); }
public CommonResponse ParseDbResultWithSql(string sql) { return ParseDbResult(ExecuteDataTable(sql)); }
public CommonResponse ParseDbResult(DataTable dt) { var res = new CommonResponse(); if (dt.Rows.Count > 0) { res.ResponseCode = dt.Rows[0][0].ToString(); res.Msg = dt.Rows[0][1].ToString(); res.Id = dt.Rows[0][2].ToString(); if (dt.Columns.Count > 3) { res.Extra = dt.Rows[0][3].ToString(); } if (dt.Columns.Count > 4) { res.Extra2 = dt.Rows[0][4].ToString(); } } return res; } public DataSet ExecuteDataSet(string sql) { var ds = new DataSet(); using (var con = new SqlConnection(GetConnectionString())) { var cmd = new SqlCommand(sql, con); cmd.CommandTimeout = GetCommandTimeOut(); SqlDataAdapter da; try { da = new SqlDataAdapter(cmd); da.Fill(ds); da.Dispose(); }
catch (Exception ex) { throw ex;
} finally { da = null; cmd.Dispose(); } return ds; } }
public DataTable ExecuteDataTable(string sql) { using (var ds = ExecuteDataSet(sql)) { if (ds == null || ds.Tables.Count == 0) return null;
return ds.Tables[0]; } }
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 String FilterString(string strVal) { var str = FilterQuote(strVal);
if (str.ToLower() != "null") str = "'" + str + "'";
return str.TrimEnd().TrimStart(); }
public String FilterXmlString(string strVal) { return "'" + strVal + "'"; }
public String FilterXmlNodeString(string strVal) { var str = FilterQuote(strVal);
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 = Regex.Replace(str, " select ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " insert ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " update ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " delete ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " drop ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " truncate ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " create ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " begin ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " end ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " char ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " exec ", string.Empty, RegexOptions.IgnoreCase); str = Regex.Replace(str, " xp_cmd ", string.Empty, RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<.*?>", string.Empty);
} else { str = "null"; } return str; }
public string ConvertDrToString(object dr) { if (dr != DBNull.Value) { return Convert.ToString(dr); } return string.Empty; }
public Decimal ConvertDrToDecimal(object dr) { if (dr != DBNull.Value) { return Convert.ToDecimal(dr.ToString()); } return 0; }
public Int64 ConvertDrToInt64(object dr) { if (dr != DBNull.Value) { return Convert.ToInt64(dr.ToString()); } return 0; }
public DateTime ConvertDrToDate(object dr) { if (dr != DBNull.Value) { return Convert.ToDateTime(dr.ToString()); } return DateTime.MinValue; }
public Int16 ConvertDrToInt(object dr) { if (dr != DBNull.Value) { return Convert.ToInt16(dr.ToString()); } return 0; }
public string ParseQuote(string val) { return "\"" + val + "\""; }
public string AutoSelect(string str1, string str2) { if (str1.ToLower() == str2.ToLower()) return "selected=\"selected\"";
return ""; }
public String GetSingleResult(string sql) { try { var ds = ExecuteDataSet(sql); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return "";
return ds.Tables[0].Rows[0][0].ToString(); } catch (Exception ex) { throw ex; }
finally { CloseConnection(); } } }
}
|