using Utility; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using DBHelper; namespace Test { //Config表的SQL脚本 //CREATE TABLE [Config]( // [Name] [varchar](800) NOT NULL, // [Value] [nvarchar](Max) NULL, // CONSTRAINT [PK_Config] PRIMARY KEY CLUSTERED //( // [Name] ASC //)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] //) ON [PRIMARY] //GO public static class Config { /// <summary> /// 用法:var createTime = Config.GetValue(ConfigString.CreateTime, new DateTime(2000, 1, 1)); /// </summary> public static T GetValue<T>(string key, T defaultVaule) { T ret = (T)Convert.ChangeType(defaultVaule, typeof(T)); try { string dbValue = GetValue(key); ret = (T)Convert.ChangeType(dbValue, typeof(T)); } catch (Exception ex) { ex.SaveLog(); } return ret; } /// <summary> /// 用法:Config.SetValue(ConfigString.CreateTime, DateTime.Now); /// <returns></returns> public static bool SetValue<T>(string key, T value) { bool result = true; try { if (Exists(key)) { result = Update(key, value); } else { result = Add(key, value); } } catch (Exception ex) { ex.SaveLog(); result = false; } return result; } #region db操作 private const string TABLE_NAME = "Config"; private static bool Exists(string Name) { string sql = string.Format("select count(1) from {0} where Name=@Name", TABLE_NAME); StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("select count(1) from {0}", TABLE_NAME); strSql.Append(" where Name= @Name"); SqlParameter[] parameters = { new SqlParameter("@Name", SqlDbType.VarChar ,800) }; parameters[0].Value = Name; return DbHelperSQL.Exists(strSql.ToString(), parameters); } private static bool Add<T>(string Name, T Value) { StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("insert into {0}(Name,Value) values (@Name,@Value)", TABLE_NAME); SqlParameter[] parameters = { new SqlParameter("@Name", SqlDbType.VarChar,50), new SqlParameter("@Value",SqlDbType.NVarChar)}; parameters[0].Value = Name; parameters[1].Value = Value.ToString(); //这里加了验证. 防止有重复时插入不进 try { return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0; } catch (Exception ex) { ex.SaveLog(); return false; } } private static bool Update<T>(string Name, T Value) { StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("update {0} set Value=@Value where Name=@Name", TABLE_NAME); SqlParameter[] parameters = { new SqlParameter("@Value",SqlDbType.NVarChar), new SqlParameter("@Name", SqlDbType.VarChar,800)}; parameters[0].Value = Value; parameters[1].Value = Name; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0; } private static string GetValue(string Name) { StringBuilder strSql = new StringBuilder(); strSql.AppendFormat("select Value from {0} where Name=@Name", TABLE_NAME); SqlParameter[] parameters = { new SqlParameter("@Name", SqlDbType.VarChar,800)}; parameters[0].Value = Name; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); return obj == null ? string.Empty : obj.ToString(); } #endregion } public class ConfigString { public const string CreateTime = "CreateTime"; } }