using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.IO; /// <summary> /// Common 的摘要说明 /// </summary> public class Common { public Common() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 检测数据库是否存在指定条件的数据 /// </summary> /// <param name="tableName">数据表名</param> /// <param name="strWhere">查询条件,需要加“where”如"where id = 7"</param> /// <returns>true:存在或false:不存在</returns> public static bool HasData(string tableName, string strWhere) { if (RecordsCount(tableName, strWhere) > 0) { return true; } else { return false; } } /// <summary> /// 得到16位MD5密码 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string GetMD5(string s) { string r = FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5"); r = r.Substring(8, 16); return r; } #region 分页相关 /// <summary> /// 获取一页的数据记录。注意:此方法需要分页存储过程“GetPageData”的支持 /// </summary> /// <param name="tableName">表名</param> /// <param name="tableID">主键</param> /// <param name="fieldName">需要返回的列,如果是多个,则以逗号间隔</param> /// <param name="pageSize">每页记录数</param> /// <param name="pageNum">当前页码</param> /// <param name="orderString">排序的条件(注意: 要加 order by,此参数可以不赋值)</param> /// <param name="whereString">查询条件 (注意: 要加 where,此参数可以不赋值)</param> public static DataSet PageData(string tableName, string tableID, string fieldName, int pageSize, int pageNum, string orderString, string whereString) { OleDbParameter[] values = new OleDbParameter[7] { new OleDbParameter("@TableName", tableName), new OleDbParameter("@TableID", tableID), new OleDbParameter("@FieldName", fieldName), new OleDbParameter("@PageSize", pageSize), new OleDbParameter("@PageNum", pageNum), new OleDbParameter("@OrderString", orderString), new OleDbParameter("@WhereString", whereString) }; return DataBase.ExecuteDataSet("GetPageData", values); } /// <summary> /// 返回符合条件的记录的页数 /// </summary> /// <param name="recordCount">记录总数量</param> /// <param name="pageSize">页大小</param> public static int PagesCount(int recordCount, int pageSize) { int result = recordCount / pageSize; if (recordCount % pageSize > 0) { result++; } return result; } /// <summary> /// 返回符合条件的记录的数目 /// </summary> /// <param name="tableName">数据表名</param> /// <param name="strWhere">查询条件,需要加“where”如"where id = 7"</param> public static int RecordsCount(string tableName, string strWhere) { return (int)DataBase.ExecuteScalar(string.Format("select count(*) from {0} {1}", tableName, strWhere)); } #endregion 分页相关 #region 上传方法 /// <summary> /// 上传文件,返回上传的文件的名称 /// </summary> /// <param name="FileUpload1">上传控件的名称</param> /// <param name="savePath">文件保存的绝对路径</param> public static string UploadPic(ref FileUpload FileUpload1, string savePath) { return UploadFile(ref FileUpload1, savePath, ".rar,.jpg,.gif,.png", 512); } /// <summary> /// 上传文件,返回上传的文件的名称 /// </summary> /// <param name="FileUpload1">上传控件的名称</param> /// <param name="savePath">文件保存的绝对路径</param> /// <param name="fileExts">允许上传文件的扩展名,若是多个,则之间以逗号间隔</param> /// <param name="maxLength">文件的上限,单位为K。例如,限制最大为512K,则可直接将此参数赋值为“512”</param> public static string UploadFile(ref FileUpload FileUpload1, string savePath, string fileExts, int maxLength) { string fileExt, fileName, newFileName = "0", filePath = savePath; fileName = FileUpload1.FileName.Trim(); if (fileName.Length > 0 && FileUpload1.PostedFile.ContentLength < maxLength * 1024) { int j = fileName.LastIndexOf('.'); fileExt = fileName.Substring(j).ToLower(); if (fileExts.IndexOf(fileExt) > -1) { Random r = new Random(); int i = r.Next(0, 1000); newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + i.ToString() + fileExt; filePath += newFileName; FileUpload1.SaveAs(filePath); } } return newFileName; } #endregion 上传方法 #region 图片控制 static string defaultImgUrl = "~/NuoWanFile/default.jpg";//默认图片 /// <summary> /// 设置默认图片:如果图片不存在则显示默认图片 /// </summary> /// <param name="img">图片控件Id</param> public static void DisDefaultImage(ref Image img) { if (img != null) { Page page = new Page(); if (!File.Exists(page.Server.MapPath(img.ImageUrl.Trim()))) { img.ImageUrl = defaultImgUrl; img.Width = 70; img.Height = 70; } } } /// <summary> /// 限制图片宽度 /// </summary> /// <param name="img">图片控件的ID</param> /// <param name="maxWidth">最大宽度</param> public static void SetImageWidth(ref Image img, int maxWidth) { if (img != null) { Page page = new Page(); string path = page.Server.MapPath(img.ImageUrl); if (File.Exists(path)) { System.Drawing.Image image = System.Drawing.Image.FromFile(path);//抽象目标图片为一个对象类型 string str = image.Width.ToString();//得到图片的实际大小 if (Convert.ToInt32(str) > maxWidth) { img.Width = maxWidth;//设置图片控件大小 } } } } #endregion 图片控制 }