1.IOHelper(FileHelper)
2.XmlHelper
3.DBHelper
4.IISHelper
5.OSHelper(WindowsHelper)
6.SqlserverHelper
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Data; usingSystem.Data.SqlClient; usingSystem.IO; namespaceWindowsDeploy.Helpers { public static classSqlServerHelper { public static int ExcuteNonQuery(string conStr, stringsqlStr) { SqlConnection con = newSqlConnection(conStr); try{ con.Open(); SqlCommand cmd = newSqlCommand(sqlStr, con); cmd.CommandType =CommandType.Text; returncmd.ExecuteNonQuery(); } catch(Exception ex) { return -1; } finally{ con.Close(); } } public static int ExcuteNonQuery(string conStr, string sqlStr, CommandType type, paramsSqlParameter[] param) { SqlConnection con = newSqlConnection(conStr); try{ con.Open(); SqlCommand cmd = newSqlCommand(sqlStr, con); cmd.CommandType =type; foreach (SqlParameter p inparam) { cmd.Parameters.Add(p); } returncmd.ExecuteNonQuery(); } catch(Exception ex) { return -1; } finally{ con.Close(); } } /// <summary> ///还原数据库文件 /// </summary> /// <param name="filePath">数据库备份文件名称路径</param> /// <param name="databaseName">需要还原的数据库名称</param> /// <param name="conStr">数据库连接</param> /// <returns></returns> public static bool RestoreDataBase(string filePath, string databaseName, stringconStr) { string fileName = filePath.Split('\\')[filePath.Split('\\').Length-1]; string basePath=filePath.Replace(fileName,""); SqlConnection conn = newSqlConnection(conStr); SqlCommand command = null; try{ string restoreStr = string.Empty; string getLogicFileName = string.Format("restore filelistonly from disk ='{0}'", filePath); DataSet ds = newDataSet(); SqlDataAdapter da = newSqlDataAdapter(getLogicFileName, conn); da.Fill(ds); if (ds.Tables == null || ds.Tables[0].Rows.Count == 0) return false; foreach (DataRow dr in ds.Tables[0].Rows) { //查找数据库逻辑文件名称 if ("D".Equals(dr["Type"].ToString())) restoreStr += string.Format("move '{0}' to '{1}',", dr["LogicalName"].ToString(), Path.Combine(basePath, databaseName) + "_Data.mdf"); //查找数据库日志文件名称 else if ("L".Equals(dr["Type"].ToString())) restoreStr += string.Format("move '{0}' to '{1}',", dr["LogicalName"].ToString(), Path.Combine(basePath, databaseName) + "_Log.ldf"); } if (string.IsNullOrEmpty(restoreStr)) restoreStr = string.Format("restore database {0} from disk ='{1}'", databaseName, Path.Combine(basePath, fileName)); else{ restoreStr = string.Format("restore database {0} from disk ='{1}' with replace,", databaseName, Path.Combine(basePath, fileName)) + restoreStr.TrimEnd(','); } command = newSqlCommand(restoreStr, conn); conn.Open(); command.ExecuteNonQuery(); return true; } catch(Exception ex) { return false; } finally{ conn.Close(); } } } }
7.AppConfigHelper
usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.Text; usinglog4net; namespacext { public classAppConfigHelper { private static ILog log = LogManager.GetLogger(typeof(AppConfigHelper)); public static int? Get(string key, int?defaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); } else{ intv; if (!int.TryParse(s, outv)) { log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key); } else{ defaultValue =v; log.InfoFormat("Get: {0}={1}", key, defaultValue); } } returndefaultValue; } public static bool? Get(string key, bool?defaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); } else{ boolv; if (!bool.TryParse(s, outv)) { log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key); } else{ defaultValue =v; log.InfoFormat("Get: {0}={1}", key, defaultValue); } } returndefaultValue; } public static T GetEnum<T>(stringkey, T defaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); returndefaultValue; } else{ T v = (T) Enum.Parse(typeof(T), s); log.InfoFormat("Get: {0}={1}", key, defaultValue); returnv; } } public static string Get(string key, stringdefaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); returndefaultValue; } log.InfoFormat("Get: {0}={1}", key, s); returns; } public static DateTime Get(stringkey, DateTime defaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); returndefaultValue; } DateTime result; if(!DateTime.TryParse(s, outresult)) { log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s); returndefaultValue; } log.InfoFormat("Get: {0}={1}", key, s); returnresult; } public static TimeSpan Get(stringkey, TimeSpan defaultValue) { string s =ConfigurationManager.AppSettings[key]; if (string.IsNullOrEmpty(s)) { log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key); returndefaultValue; } TimeSpan result; if (!TimeSpan.TryParse(s, outresult)) { log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s); returndefaultValue; } log.InfoFormat("Get: {0}={1}", key, s); returnresult; } }
陆续更新中。。。
下载地址: https://files.cnblogs.com/8090sns/DotNet.Utilities.rar