|
|
using Common.Utility; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web.Script.Serialization;
namespace Repository.DAO.SwiftDao { public class SwiftDao : ISwiftDao { SqlConnection _connection;
public SwiftDao() { Init(); }
private void Init() { _connection = new SqlConnection(GetConnectionString()); }
private string GetConnectionString() { return ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString; }
private int GetCommandTimeOut() { int cto = 0; try { int.TryParse(ConfigurationManager.AppSettings["cto"].ToString(), out cto); if (cto == 0) cto = 30; } catch (Exception ex) { cto = 30; } return cto; }
private void CloseConnection() { if (_connection.State == ConnectionState.Open) this._connection.Close(); }
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 FilterStringUnicode(string strVal) { var str = FilterQuote(strVal);
if (str.ToLower() != "null") str = "N'" + str + "'";
return str; }
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 DbResult ParseDbResult(DataTable dt) { var res = new DbResult(); if (dt.Rows.Count > 0) { res.ErrorCode = dt.Rows[0][0].ToString(); res.Msg = dt.Rows[0][1].ToString(); res.Id = dt.Rows[0][2].ToString(); if (dt.Columns.Count.Equals(3)) return res;
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 DbResult ParseDbResult(string sql) { return ParseDbResult(ExecuteDataSet(sql).Tables[0]); }
public string DataTableToJson(DataTable table) { if (table == null) return ""; var list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows) { var dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns) { dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col]; } list.Add(dict); } var serializer = new JavaScriptSerializer(); string json = serializer.Serialize(list); return json; }
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(); } }
} }
|