C

雅轩聊科技 2024-08-05 20:12:54

哈喽,你好啊,我是雷工!

除了验证是否为空以外,还需要验证身份证号和考勤卡号在系统中是否已存在,避免重复添加,或填写错误;

以下为实 现笔记。

01 效果演示

①身份证号是否存在验证

②考勤卡号是否存在验证

02 实现步骤

2.1、访问方法

首先在数据访问层的人员信息访问类PeopleServer中添加验证方法;

①判断身份证号是否存在的方法:

/// <summary>/// 判断身份证号是否存在/// </summary>/// <param name="peopleIdNo"></param>/// <returns></returns>public bool IsIdNoExisted(string peopleIdNo){ //数据库内身份证号字段类型为Char(18)时可包含X 写法: string sql1 = string.Format("select count(*)from Peoples where PeopleIdNo='{0}'",peopleIdNo); //数据库身份证号字段类型为numeric(18,0)时,写法: string sql = string.Format("select count(*)from Peoples where PeopleIdNo={0}",peopleIdNo); int result = Convert.ToInt32(SQLHelper.GetSingleResult(sql)); if (result == 1) return true; else return false;}

②判断考勤卡号是否存在的方法:

/// <summary>/// 判断考勤卡号是否存在/// </summary>/// <param name="cardNo"></param>/// <returns></returns>public bool IsChardNoExisted(string cardNo){ //数据库内身份证号字段类型为Char(18)时可包含X 写法: string sql= string.Format("select count(*)from Peoples where CardNo='{0}'",cardNo); int result = Convert.ToInt32(SQLHelper.GetSingleResult(sql)); if (result == 1) return true; else return false;}

2.2、数据访问对象

已经创建数据访问对象

private PeopleServer peopleServer = new PeopleServer();

2.3、判断提示

在添加按钮的事件中调用方法判断,并提示信息:

①:验证系统中添加的身份证号是否已存在

//验证系统中添加的身份证号是否已存在 if(peopleServer.IsIdNoExisted(this.txtPeopleIdNo.Text.Trim())) {MessageBox.Show("该身份证号在系统中已存在!", "雷工提示");this.txtPeopleIdNo.SelectAll();this.txtPeopleIdNo.Focus();return; }

②:验证系统中添加的考勤号是否已存在

//验证系统中添加的考勤卡号是否已存在 if (peopleServer.IsIdNoExisted(this.txtCarNo.Text.Trim())) {MessageBox.Show("该考勤卡号在系统中已存在!", "雷工提示");this.txtPeopleIdNo.SelectAll();this.txtPeopleIdNo.Focus();return; }

03 后记

以上为系统中是否存在身份证号,考勤卡号的验证方式。

你在项目中还遇到过哪些验证?欢迎在评论区或交流群中讨论。

0 阅读:0

雅轩聊科技

简介:感谢大家的关注