身份证号检测:
/// <summary> /// 检验身份证号是否正确 /// </summary> /// <param name="Id"></param> /// <returns></returns> public static bool CheckIDCard(string Id) { if (Id.Length == 18) { bool check = CheckIDCard18(Id); return check; } else if (Id.Length == 15) { bool check = CheckIDCard15(Id); return check; } else { return false; } } /// <summary> /// 18位身份证验证 /// </summary> /// <param name="Id">身份证号</param> /// <returns></returns> public static bool CheckIDCard18(string Id) { try { long n = 0; if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false) { return false;//数字验证 } string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2)) == -1) { return false;//省份验证 } string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-"); DateTime time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false;//生日验证 } string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); char[] Ai = Id.Remove(17).ToCharArray(); int sum = 0; for (int i = 0; i < 17; i++) { sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString()); } int y = -1; Math.DivRem(sum, 11, out y); if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower()) { return false;//校验码验证 } return true;//符合GB11643-1999标准} } catch { return false; } } /// <summary> /// 15位身份证验证 /// </summary> /// <param name="Id">身份证号</param> /// <returns></returns> public static bool CheckIDCard15(string Id) { try { long n = 0; if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14)) { return false;//数字验证 } string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2)) == -1) { return false;//省份验证 } string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-"); DateTime time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false;//生日验证 } return true;//符合15位身份证标准 } catch { return false; } } /// <summary> /// 检查身份证号是否存在 /// </summary> /// <param name="card">身份证号</param> /// <param name="tableName">表名</param> /// <returns></returns> public bool CheckCardIsExist(string card, string tableName, string strWhere) { string strSql = " select count(1) from " + tableName + " where Shenfenzheng='" + card + "'"; if (strWhere != "") { strSql += " and " + strWhere; } if (int.Parse(DBUtility.DbHelperSQL.GetSingle(strSql).ToString()) > 0) { return false; } else { return true; } }
#region 将字符串转换为数组 public static string[] GetStrArray(string str) { return str.Split(new char[',']); } #endregion #region 将数组转换为字符串 public static string GetArrayStr(List<string> list, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i == list.Count - 1) { sb.Append(list[i]); } else { sb.Append(list[i]); sb.Append(speater); } } return sb.ToString(); } #endregion #region 删除最后结尾的一个逗号 /// <summary> /// 删除最后结尾的一个逗号 /// </summary> public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } #endregion #region 删除最后结尾的指定字符后的字符 /// <summary> /// 删除最后结尾的指定字符后的字符 /// </summary> public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } #endregion #region 生成指定长度的字符串 /// <summary> /// 生成指定长度的字符串,即生成strLong个str字符串 /// </summary> /// <param name="strLong">生成的长度</param> /// <param name="str">以str生成字符串</param> /// <returns></returns> public static string StringOfChar(int strLong, string str) { string ReturnStr = ""; for (int i = 0; i < strLong; i++) { ReturnStr += str; } return ReturnStr; } #endregion #region 生成日期随机码 /// <summary> /// 生成日期随机码 /// </summary> /// <returns></returns> public static string GetRamCode() { #region return DateTime.Now.ToString("yyyyMMddHHmmssffff"); #endregion } #endregion #region 截取字符长度 /// <summary> /// 截取字符长度 /// </summary> /// <param name="inputString">字符</param> /// <param name="len">长度</param> /// <returns></returns> public static string CutString(string inputString, int len) { ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (mybyte.Length > len) tempString += "…"; return tempString; } #endregion #region 清除HTML标记 public static string DropHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([ ])[s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", """, RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "xa1", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "xa2", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "xa3", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", ""); Htmlstring.Replace(">", ""); Htmlstring.Replace(" ", ""); Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring; } #endregion #region 清除HTML标记且返回相应的长度 public static string DropHTML(string Htmlstring, int strLen) { return CutString(DropHTML(Htmlstring), strLen); } #endregion #region TXT代码转换成HTML格式 /// <summary> /// 字符串字符处理 /// </summary> /// <param name="chr">等待处理的字符串</param> /// <returns>处理后的字符串</returns> /// //把TXT代码转换成HTML格式 public static String ToHtml(string Input) { StringBuilder sb = new StringBuilder(Input); sb.Replace("&", "&"); sb.Replace("<", "<"); sb.Replace(">", ">"); sb.Replace(" ", "<br />"); sb.Replace(" ", "<br />"); sb.Replace(" ", " "); //sb.Replace(" ", " "); return sb.ToString(); } #endregion #region HTML代码转换成TXT格式 /// <summary> /// 字符串字符处理 /// </summary> /// <param name="chr">等待处理的字符串</param> /// <returns>处理后的字符串</returns> /// //把HTML代码转换成TXT格式 public static String ToTxt(String Input) { StringBuilder sb = new StringBuilder(Input); sb.Replace(" ", " "); sb.Replace("<br>", " "); sb.Replace("<br>", " "); sb.Replace("<br />", " "); sb.Replace("<br />", " "); sb.Replace("<", "<"); sb.Replace(">", ">"); sb.Replace("&", "&"); return sb.ToString(); } #endregion #region 检查危险字符 /// <summary> /// 检查危险字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Filter(string sInput) { if (sInput == null || sInput == "") return null; string sInput1 = sInput.ToLower(); string output = sInput; string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'"; if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) { throw new Exception("字符串中含有非法字符!"); } else { output = output.Replace("'", "''"); } return output; } #endregion #region 检查过滤设定的危险字符 /// <summary> /// 检查过滤设定的危险字符 /// </summary> /// <param name="InText">要过滤的字符串 </param> /// <returns>如果参数存在不安全字符,则返回true </returns> public static bool SqlFilter(string word, string InText) { if (InText == null) return false; foreach (string i in word.Split('|')) { if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1)) { return true; } } return false; } #endregion #region 过滤特殊字符 /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Htmls(string Input) { if (Input != string.Empty && Input != null) { string ihtml = Input.ToLower(); ihtml = ihtml.Replace("<script", "<script"); ihtml = ihtml.Replace("script>", "script>"); ihtml = ihtml.Replace("<%", "<%"); ihtml = ihtml.Replace("%>", "%>"); ihtml = ihtml.Replace("<$", "<$"); ihtml = ihtml.Replace("$>", "$>"); return ihtml; } else { return string.Empty; } } #endregion #region 获得当前绝对路径 /// <summary> /// 获得当前绝对路径 /// </summary> /// <param name="strPath">指定的路径</param> /// <returns>绝对路径</returns> public static string GetMapPath(string strPath) { if (strPath.ToLower().StartsWith("http://")) { return strPath; } if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(strPath); } else //非web程序引用 { strPath = strPath.Replace("/", "\"); if (strPath.StartsWith("\")) { strPath = strPath.Substring(strPath.IndexOf('\', 1)).TrimStart('\'); } return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } } #endregion /// <summary> /// 写cookie值 /// </summary> /// <param name="strName">名称</param> /// <param name="strValue">值</param> public static void WriteCookie(string strName, string strValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie.Value = strValue; HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 写cookie值 /// </summary> /// <param name="strName">名称</param> /// <param name="strValue">值</param> public static void WriteCookie(string strName, string key, string strValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie[key] = strValue; HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 写cookie值 /// </summary> /// <param name="strName">名称</param> /// <param name="strValue">值</param> /// <param name="strValue">过期时间(分钟)</param> public static void WriteCookie(string strName, string strValue, int expires) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie.Value = strValue; cookie.Expires = DateTime.Now.AddMinutes(expires); HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 读cookie值 /// </summary> /// <param name="strName">名称</param> /// <returns>cookie值</returns> public static string GetCookie(string strName) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) return HttpContext.Current.Request.Cookies[strName].Value.ToString(); return ""; } /// <summary> /// 读cookie值 /// </summary> /// <param name="strName">名称</param> /// <returns>cookie值</returns> public static string GetCookie(string strName, string key) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) return HttpContext.Current.Request.Cookies[strName][key].ToString(); return ""; } public static string FormatLastString(string _in) { return DelLastChar(_in, ","); } public static string FormatLastString(string _in, string _sep) { return DelLastChar(_in, _sep); } public static string GetRanDomFileName() { return DateTime.Now.ToString("yyyyMMddhhmmss"); } #region MyRegion 弹出框并跳转到指定页面 /// <summary> /// 弹出框并跳转到指定页面 /// </summary> /// <param name="txtMessage">弹出框提示信息</param> /// <param name="url">要跳转到的地址</param> public void MessageBox(string txtMessage, string url) { Page page = (Page)HttpContext.Current.CurrentHandler; page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('" + txtMessage + "');window.location='" + url + "'", true); } #endregion /// <summary> /// 判断最后结尾是否为指定字符,是则删除 /// </summary> /// <param name="str"></param> /// <param name="strchar"></param> /// <returns></returns> public static string DelLastComma(string str,string strchar) { string s = str.Substring(str.Length - 1, 1); if (s == strchar) { return str.Substring(0, str.Length - 1); } else { return str; } }
DataTable2Json:
public string DataTable2Json(DataTable dt) { if (dt != null && dt.Rows.Count > 0) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{"); jsonBuilder.Append(""ds""); jsonBuilder.Append(":["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append(""" + dt.Columns[j].ColumnName + """); jsonBuilder.Append(":""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("}"); return jsonBuilder.ToString(); } else { return "{"ds":{}}"; } }
获取数据列表:
/// <summary> /// 获得数据列表(按页码,每页20条) /// </summary> public DataSet GetListresult(int startId, string tableName, string strWhere, string filedOrder) { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT * FROM ( "); strSql.Append(" SELECT ROW_NUMBER() OVER ("); if (!string.IsNullOrEmpty(filedOrder.Trim())) { strSql.Append("order by T." + filedOrder); } else { strSql.Append("order by T.ID desc"); } strSql.Append(")AS R, T.* from " + tableName + " T "); if (!string.IsNullOrEmpty(strWhere.Trim())) { strSql.Append(" WHERE " + strWhere); } strSql.Append(" ) TT"); strSql.AppendFormat(" WHERE TT.R between {0} and {1}", 20 * (startId - 1) + 1, 20 * startId); return OracleHelper.ExecuteDataset(strSql.ToString()); } /// <summary> /// 获得数据列表(前几条) /// </summary> public DataSet GetList(int top, string tableName, string strWhere, string filedOrder) { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT * FROM ( "); strSql.Append(" SELECT ROW_NUMBER() OVER ("); if (!string.IsNullOrEmpty(filedOrder.Trim())) { strSql.Append("order by T." + filedOrder); } else { strSql.Append("order by T.ID desc"); } strSql.Append(")AS R, T.* from " + tableName + " T "); if (!string.IsNullOrEmpty(strWhere.Trim())) { strSql.Append(" WHERE " + strWhere); } strSql.Append(" ) TT"); strSql.AppendFormat(" WHERE TT.R between {0} and {1}", 0, top); return OracleHelper.ExecuteDataset(strSql.ToString()); } /// <summary> /// 获得数据列表 /// </summary> /// <param name="top">第几条</param> /// <param name="tableName">表名</param> /// <param name="strWhere">条件</param> /// <param name="filedOrder">按哪个字段,怎样排序 createtime desc</param> /// <param name="leftjoin">左连接语句 left join userinfo u on T.user_id=u.user_id</param> /// <param name="display">要显示的字段</param> /// <returns></returns> public DataSet GetNewsList(int top, string tableName, string strWhere, string filedOrder, string leftjoin, string display) { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT * FROM ( "); strSql.Append(" SELECT ROW_NUMBER() OVER ("); if (!string.IsNullOrEmpty(filedOrder.Trim())) { strSql.Append("order by T." + filedOrder); } else { strSql.Append("order by T.ID desc"); } strSql.Append(")AS R, " + display + " from " + tableName + " T " + leftjoin); if (!string.IsNullOrEmpty(strWhere.Trim())) { strSql.Append(" WHERE " + strWhere); } strSql.Append(" ) TT"); strSql.AppendFormat(" WHERE TT.R between {0} and {1}", 0, top); return OracleHelper.ExecuteDataset(strSql.ToString()); }
/// <summary> /// 获得查询分页数据 /// </summary> public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder) { int topSize = pageSize * currentPage; StringBuilder strSql = new StringBuilder(); strSql.Append("select top " + pageSize + " Id,USERNAME,DANWEI,TEL,ADDRESS,USERIP,CREATETIME,TITLE,CONTENT,REPLYTIME,REPLYCONTENT,FLAG,PARENTID,ISDISPLAY from dt_ASK"); strSql.Append(" where Id not in(select top " + topSize + " Id from dt_ASK"); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } strSql.Append(" order by " + filedOrder + ")"); if (strWhere.Trim() != "") { strSql.Append(" and " + strWhere); } strSql.Append(" order by " + filedOrder); return DbHelperSQL.Query(strSql.ToString()); }
<img src="data:image/gif;base64,你的二进制流" alt=""/>
EncodeJsString:
/// <summary> /// Encodes a string to be represented as a string literal. The format /// is essentially a JSON string. /// /// The string returned includes outer quotes /// Example Output: "Hello "Rick"! Rock on" /// </summary> /// <param name="s"></param> /// <returns></returns> public static string EncodeJsString(string s) { StringBuilder sb = new StringBuilder(); sb.Append("""); foreach (char c in s) { switch (c) { case '"': sb.Append("\""); break; case '\': sb.Append("\\"); break; case '': sb.Append("\b"); break; case 'f': sb.Append("\f"); break; case ' ': sb.Append("\n"); break; case ' ': sb.Append("\r"); break; case ' ': sb.Append("\t"); break; default: int i = (int)c; if (i < 32 || i > 127) { sb.AppendFormat("\u{0:X04}", i); } else { sb.Append(c); } break; } } sb.Append("""); return sb.ToString(); }
.aspx:
<img src='<%# GetStatusPicture(Container.DataItem)%>' alt="Status" />
.aspx.cs:
protected string GetStatusPicture(object dataItem) { int units = Int32.Parse(DataBinder.Eval(dataItem,"UnitsInStock").ToString()); if (units == 0) return "Cancel.gif"; else if (units > 50) return "Ok.gif"; else return "blank.gif"; }
DataView的用法:
DataSet ds = new DataSet(); DataView dv = new DataView(); dv = ds.Tables["TABLE1"].DefaultView; dv.RowFilter = "字段=条件";//多条件用and连接 repeater1.DataSource = dv;
<a href="<%#Eval("YEWUISLAST") == 0 ? "XXX.aspx?pid="+Eval("YEWUID") : Eval("YEWUMULU").ToString()%>"><%#Eval("YEWUNAME") %></a>