public static AppSetting GetSettingByKey(string keyName) { DbProviderFactory myProv = Database.ProviderFactory; DbConnection myConn = myProv.CreateConnection(); DbDataReader myRd = null; DbCommand myCmd = myConn.CreateCommand(); string sSql; AppSetting result = new AppSetting(); try { myConn.ConnectionString = Database.ConnString; myConn.Open(); myCmd.Connection = myConn; sSql = "SELECT KeySet, KeyName, KeyTitle, KeyValue, KeyInfo " + " FROM #__appSettings m " + " WHERE KeySet = @KeySet AND KeyName = @KeyName "; myCmd.CommandText = Database.ParseSql(sSql); myCmd.Parameters.Add(Database.Parameter(myProv, "KeySet", KEYSET)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyName", keyName)); myRd = myCmd.ExecuteReader(); if (myRd.Read()) { if (!Convert.IsDBNull(myRd["KeySet"])) result.KeySet = (string)myRd["KeySet"]; if (!Convert.IsDBNull(myRd["KeyName"])) result.KeyName = (string)myRd["KeyName"]; if (!Convert.IsDBNull(myRd["KeyTitle"])) result.KeyTitle = (string)myRd["KeyTitle"]; if (!Convert.IsDBNull(myRd["KeyValue"])) result.KeyValue = (string)myRd["KeyValue"]; if (!Convert.IsDBNull(myRd["KeyInfo"])) result.KeyInfo = (string)myRd["KeyInfo"]; } myRd.Close(); } finally { myConn.Dispose(); } return result; }
public static AppSetting Insert(AppSetting newObj) { DbProviderFactory myProv = Database.ProviderFactory; DbConnection myConn = myProv.CreateConnection(); DbCommand myCmd = myConn.CreateCommand(); string sSql; AppSetting result = new AppSetting(); try { myConn.ConnectionString = Database.ConnString; myConn.Open(); myCmd.Connection = myConn; result.KeyName = newObj.KeyName; result.KeyTitle = newObj.KeyTitle; result.KeyValue = newObj.KeyValue; result.KeyInfo = newObj.KeyInfo; sSql = "INSERT INTO #__AppSettings(KeySet, KeyName, KeyTitle, KeyValue, KeyInfo) " + "VALUES(@KeySet, @KeyName, @KeyTitle, @KeyValue, @KeyInfo) "; myCmd.CommandText = Database.ParseSql(sSql); myCmd.Parameters.Add(Database.Parameter(myProv, "KeySet", result.KeySet)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyName", result.KeyName)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyTitle", result.KeyTitle)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyValue", result.KeyValue)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyInfo", result.KeyInfo)); myCmd.ExecuteNonQuery(); refreshApplicationVar(result.KeyName, result.KeyValue); } finally { myConn.Dispose(); } return result; }
public static int Update(AppSetting theObj) { DbProviderFactory myProv = Database.ProviderFactory; DbConnection myConn = myProv.CreateConnection(); DbCommand myCmd = myConn.CreateCommand(); string sSql; int result = 0; try { myConn.ConnectionString = Database.ConnString; myConn.Open(); myCmd.Connection = myConn; sSql = "UPDATE #__appSettings SET KeyTitle=@KeyTitle, KeyValue=@KeyValue, KeyInfo=@KeyInfo " + " WHERE KeySet=@KeySet AND KeyName=@KeyName "; myCmd.CommandText = Database.ParseSql(sSql); myCmd.Parameters.Add(Database.Parameter(myProv, "KeySet", theObj.KeySet)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyName", theObj.KeyName)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyTitle", theObj.KeyTitle)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyValue", theObj.KeyValue)); myCmd.Parameters.Add(Database.Parameter(myProv, "KeyInfo", theObj.KeyInfo)); result = myCmd.ExecuteNonQuery(); refreshApplicationVar(theObj.KeyName, theObj.KeyValue); } finally { myConn.Dispose(); } return result; }
/// <summary> /// retrieve AppSetting Value /// </summary> /// <param name="keyName"></param> /// <returns></returns> public static string GetValue(string keyName, string defaultValue = "") { string result = ""; Object obj = HttpContext.Current.Application[keyName]; if (obj == null) { AppSetting appSetting = new AppSetting(); appSetting = AppSettingsManager.GetSettingByKey(keyName); refreshApplicationVar(appSetting.KeyName, appSetting.KeyValue); result = appSetting.KeyValue; if (string.IsNullOrEmpty(result)) { PigeonCms.Tracer.Log("Missing AppSetting '" + keyName + "'", TracerItemType.Error); } } else { result = obj.ToString(); } if (string.IsNullOrEmpty(result)) result = defaultValue; return result; }