是从一个asp.net mvc的项目里看到的。挺实用的。


通过身份证号码获取出生日期和性别




通过身份证号码获取出生日期和性别

  1. #region 由身份证获得出生日期
  2. public static string CardBrith(string CardNo)
  3. {
  4. return CardNo.Substring(, ) + "-" + CardNo.Substring(, ) + "-" + CardNo.Substring(, );
  5. }
  6. #endregion
  7.  
  8. #region 由身份证获得性别
  9. public static string CardSex(string CardNo)
  10. {
  11. string sSex = "";
  12. int sex = Convert.ToInt16(CardNo.Substring(, ));
  13. if (sex % == )//性别代码为偶数是女性奇数为男性
  14. {
  15. sSex = "女";
  16. }
  17. else
  18. {
  19. sSex = "男";
  20. }
  21. return sSex;
  22. }
  23. #endregion






验证身份证号码:

  1. #region 验证身份证号码
  2. /// <summary>
  3. /// 验证身份证号码,正确返回true
  4. /// </summary>
  5. /// <param name="Id"></param>
  6. /// <returns></returns>
  7. public static bool CardNO(string Id)
  8. {
  9.  
  10. long n = ;
  11.  
  12. if (long.TryParse(Id.Remove(), out n) == false || n < Math.Pow(, ) || long.TryParse(Id.Replace('x', '').Replace('X', ''), out n) == false)
  13. {
  14.  
  15. return false;//数字验证
  16.  
  17. }
  18.  
  19. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  20.  
  21. if (address.IndexOf(Id.Remove()) == -)
  22. {
  23.  
  24. return false;//省份验证
  25.  
  26. }
  27.  
  28. string birth = Id.Substring(, ).Insert(, "-").Insert(, "-");
  29.  
  30. DateTime time = new DateTime();
  31.  
  32. if (DateTime.TryParse(birth, out time) == false)
  33. {
  34.  
  35. return false;//生日验证
  36.  
  37. }
  38.  
  39. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  40.  
  41. string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  42.  
  43. char[] Ai = Id.Remove().ToCharArray();
  44.  
  45. int sum = ;
  46.  
  47. for (int i = ; i < ; i++)
  48. {
  49.  
  50. sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  51.  
  52. }
  53.  
  54. int y = -;
  55.  
  56. Math.DivRem(sum, , out y);
  57.  
  58. if (arrVarifyCode[y] != Id.Substring(, ).ToLower())
  59. {
  60.  
  61. return false;//校验码验证
  62.  
  63. }
  64.  
  65. return true;//符合GB11643-1999标准
  66.  
  67. }
  68. #endregion






字符串解码

  1. #region 字符串解码
  2. /// <summary>
  3. /// 字符串解码
  4. /// </summary>
  5. /// <param name="str">原字符</param>
  6. /// <returns>解码后的字符</returns>
  7. public static string StrDecode(string str)
  8. {
  9. if (string.IsNullOrEmpty(str)) { return str; }
  10. return System.Web.HttpUtility.HtmlDecode(str); ;
  11. }
  12. #endregion







字符串编码

  1. #region 字符串编码
  2. /// <summary>
  3. /// 目前只做字符串编码
  4. /// </summary>
  5. /// <param name="str">原字符</param>
  6. /// <returns>编码后的字符</returns>
  7. public static string StrEncode(string str)
  8. {
  9. if (string.IsNullOrEmpty(str)) { return str; }
  10. return System.Web.HttpUtility.HtmlEncode(str);
  11. }
  12. #endregion






获得分页记录

  1. #region 获得分页记录
  2. /// <summary>
  3. /// 获得分页记录,返回DataTable类型
  4. /// </summary>
  5. /// <param name="tblName">表名</param>
  6. /// <param name="strGetFields">需返回的字段</param>
  7. /// <param name="strWhere">条件</param>
  8. /// <param name="OrderType">排序字段</param>
  9. /// <param name="PageIndex">当前页面</param>
  10. /// <param name="PageSize">页尺寸</param>
  11. /// <returns>返回DataTable类型</returns>
  12. public static DataTable GetPageData(string tblName, string strGetFields, string strWhere, string OrderType, int PageIndex, int PageSize)
  13. {
  14. SqlParameter[] para = {
  15. new SqlParameter("@Table", SqlDbType.VarChar, ){Value=tblName},//表名
  16. new SqlParameter("@Fields", SqlDbType.VarChar, ){Value=strGetFields},//返回的列
  17. new SqlParameter("@Where", SqlDbType.VarChar, ){Value=strWhere},//where条件
  18. new SqlParameter("@OrderBy",SqlDbType.VarChar, ){Value=OrderType},//排序
  19. new SqlParameter("@CurrentPage",SqlDbType.Int,){Value=PageIndex},//页码
  20. new SqlParameter("@PageSize", SqlDbType.Int,){Value=PageSize}//页尺寸
  21. };
  22. DataTable dt = new DataTable();
  23. XBSQL.RunProc("Pages", out dt, para);
  24. return dt;
  25. }
  26. #endregion






获得符合条件指定列的和

  1. #region 获得符合条件指定列的和
  2. /// <summary>
  3. /// 返回符合条件指定列的和
  4. /// </summary>
  5. /// <param name="Field">列</param>
  6. /// <param name="TableName">表名</param>
  7. /// <param name="WhereSql">条件</param>
  8. /// <returns></returns>
  9. public static double RsSum(string Field, string TableName, string WhereSql)
  10. {
  11. string Num = XBSQL.EScalar("select sum(" + Field + ") from " + TableName + " where " + WhereSql + "").ToString();
  12. Num = (string.IsNullOrEmpty(Num)) ? "" : Num;
  13. return Convert.ToDouble(Num);
  14. }
  15.  
  16. #endregion






获取记录总和

  1. #region 获得记录总数
  2. /// <summary>
  3. /// 获得满足条件的记录总数
  4. /// </summary>
  5. /// <param name="TableName">表名</param>
  6. /// <param name="WhereSql">条件</param>
  7. /// <returns></returns>
  8. public static int RsCount(string TableName, string WhereSql)
  9. {
  10. int i = ;
  11. int.TryParse(XBSQL.EScalar("select Count(*) from " + TableName + " where " + WhereSql + "").ToString(), out i);
  12. return i;
  13. //return Convert.ToInt32(XBSQL.EScalar("select Count(*) from " + TableName + " where " + WhereSql + ""));
  14. }
  15. #endregion
  1.  

  1.  

  1.  

  1.  

  1.  

  1.  

  1.  
  2. 根据参数读取数据库,获取多条记录单个字段
  1. #region 根据参数读取数据库,获取多条记录单个字段
  2. /// <summary>
  3. /// 获取多条记录单个字段
  4. /// </summary>
  5. /// <param name="field"></param>
  6. /// <param name="table"></param>
  7. /// <param name="where"></param>
  8. /// <returns></returns>
  9. public static string RDSQL(string field, string table, string where)
  10. {
  11. StringBuilder Str = new StringBuilder();
  12. try
  13. {
  14. string sql = "select " + field + " from " + table + " where " + where + "";
  15. SqlDataReader dr = XBSQL.EReader(sql);
  16. while (dr.Read())
  17. {
  18. Str.Append("," + dr[].ToString().Trim());
  19. }
  20. dr.Close();
  21. if (Str.Length > )
  22. {
  23. Str.Remove(, );
  24. return Str.ToString();
  25. }
  26. else
  27. {
  28. return "";
  29. }
  30. }
  31. catch (Exception Error)
  32. {
  33.  
  34. }
  35. finally
  36. {
  37. Str.Length = ;
  38. }
  39. return "";
  40. }
  41. #endregion








字符串截取

  1. #region 字符串截取
  2. /// <summary>
  3. /// str为要进行截取的字符串,start是第一个关键字(字符串),last是第二个关键字(字符串),n截取字符串方式
  4. /// </summary>
  5. /// <param name="str"></param>
  6. /// <param name="start"></param>
  7. /// <param name="last"></param>
  8. /// <param name="n"></param>
  9. /// <returns></returns>
  10. public static string GetContent(string str, string start, string last, int n)
  11. {
  12. string ResStr = "";
  13. if (str.ToLower().IndexOf(start.ToLower()) >= )
  14. {
  15. if (str.ToLower().IndexOf(last.ToLower()) >= )
  16. {
  17. switch (n)
  18. {
  19. //左右都截取(都取前面)(包含关键字)
  20. case : ResStr = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower()));
  21. ResStr = ResStr.Substring(, ResStr.ToLower().IndexOf(last.ToLower()) + last.Length); break;
  22. //左右都截取(都取前面)(去除关键字)
  23. case : ResStr = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length);
  24. ResStr = ResStr.Substring(, ResStr.ToLower().IndexOf(last.ToLower())); break;
  25. default: ResStr = ""; break;
  26. }
  27. }
  28. }
  29. return ResStr;
  30. }
  31. public static string GetContent(string str, string start, int n)
  32. {
  33. string ResStr = "";
  34. switch (n)
  35. {
  36. //只往左截取(取前面的)(包含关键字)
  37. case : ResStr = str.Substring(, str.ToLower().IndexOf(start.ToLower()) + start.Length); break;
  38. //只往左截取(取前面的)(去除关键字)
  39. case :
  40. int StrIndex = str.ToLower().IndexOf(start.ToLower());
  41. if (StrIndex > )
  42. {
  43. ResStr = str.Substring(, StrIndex);
  44. }
  45. break;
  46. //只往右截取(取前面的)(包含关键字)
  47. case : ResStr = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower())); break;
  48. //只往右截取(取前面的)(去除关键字)
  49. case : ResStr = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length); break;
  50. default: ResStr = ""; break;
  51. }
  52. return ResStr;
  53. }
  54. #endregion






判断ip格式

  1. #region 判断是否是IP格式
  2. /// <summary>
  3. /// 判断是否是IP地址格式 0.0.0.0
  4. /// </summary>
  5. /// <param name="str1">待判断的IP地址</param>
  6. /// <returns>true or false</returns>
  7. public static bool IsIPAddress(string str1)
  8. {
  9. if (str1 == null || str1 == string.Empty || str1.Length < || str1.Length > ) return false;
  10. string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
  11. Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
  12. return regex.IsMatch(str1);
  13. }
  14. #endregion






获取ip

  1. #region 获得IP
  2. /// <summary>
  3. /// 取得客户端真实IP。如果有代理则取第一个非内网地址
  4. /// </summary>
  5. public static string IPAddress
  6. {
  7. get
  8. {
  9. string result = String.Empty;
  10. result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  11. if (result != null && result != String.Empty)
  12. {
  13. //可能有代理
  14. if (result.IndexOf(".") == -) //没有"."肯定是非IPv4格式
  15. result = null;
  16. else
  17. {
  18. if (result.IndexOf(",") != -)
  19. {
  20. //有",",估计多个代理。取第一个不是内网的IP。
  21. result = result.Replace(" ", "").Replace("\"", "");
  22. string[] temparyip = result.Split(",;".ToCharArray());
  23. for (int i = ; i < temparyip.Length; i++)
  24. {
  25. if (IsIPAddress(temparyip[i])
  26. && temparyip[i].Substring(, ) != "10."
  27. && temparyip[i].Substring(, ) != "192.168"
  28. && temparyip[i].Substring(, ) != "172.16.")
  29. {
  30. return temparyip[i]; //找到不是内网的地址
  31. }
  32. }
  33. }
  34. else if (IsIPAddress(result)) //代理即是IP格式
  35. return result;
  36. else
  37. result = null; //代理中的内容 非IP,取IP
  38. }
  39. }
  40. string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  41. if (null == result || result == String.Empty)
  42. result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  43. if (result == null || result == String.Empty)
  44. result = HttpContext.Current.Request.UserHostAddress;
  45. return result;
  46. }
  47. }
  48. #endregion







转换成日期格式

  1. #region 转换成日期格式
  2. /// <summary>
  3. /// 转换成日期格式,非日期返回空值
  4. /// </summary>
  5. /// <param name="Str">值</param>
  6. /// <returns></returns>
  7. public static string ToDate(string Str, string Format)
  8. {
  9. DateTime LDate;
  10. DateTime dt1 = DateTime.Parse("1900-01-01");
  11. DateTime dt2 = DateTime.Parse("9999-12-31");
  12. if (DateTime.TryParse(Str, out LDate) && DateTime.Compare(LDate, dt1) > && DateTime.Compare(LDate, dt2) < )
  13. {
  14. Str = LDate.ToString("" + Format + "");
  15. }
  16. else
  17. {
  18. Str = "";
  19. }
  20. return Str.Trim();
  21. }
  22. #endregion

c#一些常用的方法集合的更多相关文章

  1. Dynamics CRM 常用 JS 方法集合

    JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...

  2. Dynamics CRM 常用 C# 方法集合

    Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...

  3. javascript技巧及常用事件方法集合(全)

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  4. 常用js方法集合

    var func={ //对象转jsonstring getJsonStr: function(jsonObj) { var temp = []; for (var key in jsonObj) { ...

  5. 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))

    1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...

  6. (转)Android之常用功能方法大集合

    这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...

  7. 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法

    具体见第三阶段scala-day01中的文档(scala编程基础---基础语法)  1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...

  8. WebAPi添加常用扩展方法及思维发散

    前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...

  9. C#操作XML方法集合

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

随机推荐

  1. ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

    A. Trip For Meal Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. ...

  2. SpringMVC学习记录三——8 springmvc和mybatis整合

    8      springmvc和mybatis整合 8.1      需求 使用springmvc和mybatis完成商品列表查询. 8.2      整合思路 springmvc+mybaits的 ...

  3. 调用jquery的resize方法改变div的宽度和高度在IE中不变,在谷歌中可以正常显示

    1.jquery代码: 1.1问题的版本: $(function() { haituheight(); $(window).resize(function(){ haituheight(); }); ...

  4. vue webpack多页面构建

    项目示例地址: https://github.com/ccyinghua/webpack-multipage 项目运行: 下载项目之后 # 下载依赖 npm install # 运行 npm run ...

  5. 简析--UUID

    内容转载自:http://www.cnblogs.com/java-class/p/4727698.html 阅读目录 1.UUID简介 2.UUID组成 3.UUID实战演练 1.UUID 简介 U ...

  6. Spring-Day02-依赖注入-作业

    配置beans约束自动提示 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html中打开xsd-configuratio ...

  7. c#数据库连接池

    因为使用习惯的问题,我封装了一个数据库连接池Hikari,这是我自定义的数据库连接池.因为c#的连接池按照规范的ADO.NET里面实现定义的,由数据库官方提供,但是实现方式就不知道了,反正没有看出来, ...

  8. setLocale的一个用处

    setLocale是C库中的一个设置地域化信息的C函数. 函数原型为: char *setlocale(int category, const char *locale) 参数解释: category ...

  9. HTML5读取input[type=file]中的图片

    转载 https://blog.csdn.net/fd214333890/article/details/71250488

  10. 路由器基础设置之ospf

    我们将以上面的拓扑图来进行实验,要用ospf的协议达到全网互通的效果 router1: enable 进入特权模式 config t 进入全局配置模式 interface L0 ip address ...