[C#] 操作数据库时检验性别、年龄、邮箱、手机号的输入
2016-05-20 18:34
706 查看
我们在添加或编辑数据库记录时,毫无疑问,都需要检验各个输入的格式是否满足要求。
这里首先列出判断性别、年龄、邮箱、手机号输入是否符合规范的函数,其中引出正则表达式这个知识点,最后举出实际添加记录的例子。
1. 判断性别输入:
这里用到正则表达式,需要包含一个命名空间:System.Text.RegularExpressions
更详细的使用见:C#中正则表达式的使用 /article/4712025.html
这里也有 C#正则表达式语法大全,很有帮助,建议收藏:http://www.360doc.com/content/12/0225/13/19147_189500941.shtml
返回值:如果找到该字符串,则为 value 的从零开始的索引位置;如果未找到该字符串,则为
-1。如果 value 为 String.Empty,则返回值为
0。
5. 在数据库中添加记录:
谢谢阅读,欢迎交流
这里首先列出判断性别、年龄、邮箱、手机号输入是否符合规范的函数,其中引出正则表达式这个知识点,最后举出实际添加记录的例子。
1. 判断性别输入:
private bool ifSex(string s) { if (s == "男") return true; else if (s == "女") return true; else return false; }2. 判断年龄是否为数字且大于0:
这里用到正则表达式,需要包含一个命名空间:System.Text.RegularExpressions
更详细的使用见:C#中正则表达式的使用 /article/4712025.html
这里也有 C#正则表达式语法大全,很有帮助,建议收藏:http://www.360doc.com/content/12/0225/13/19147_189500941.shtml
private bool ifAge(string a) { if (Regex.IsMatch(a, @"^[0-9]\d+$")) //判断年龄是否为数字 if (Convert.ToInt32(a) > 0) //判断年龄是否大于0 return true; else return false; else return false; }3. 判断邮箱输入是否含“@”
VS的帮助中,String.IndexOf 方法详述: https://msdn.microsoft.com/zh-cn/library/k8b1470s(v=vs.110).aspx
返回值:如果找到该字符串,则为 value 的从零开始的索引位置;如果未找到该字符串,则为-1。如果 value 为 String.Empty,则返回值为
0。
private bool ifEmail(string e) { if (e.IndexOf("@") > 0) //邮件地址必须有@,且@一般不在第一位 return true; else return false; }4. 判断手机号是否为11位数
if (strPhone.Length == 11) //手机号必须11位数 也可以使用正则表达式 "^1[0-9]{10}$",表示1开头,后跟10位其它数字
5. 在数据库中添加记录:
<span style="white-space:pre"> </span>string strPath = Application.StartupPath + "\\StudentDB.accdb"; Constr = "Provider=Microsoft.ACE.OLEDB.12.0; Data source=" + strPath; Olecon = new OleDbConnection(Constr); StringBuilder strSQL = new StringBuilder(); strSQL.Append("insert into Student(Sno, Sname, Ssex, Sage, Sphone, Semail)"); strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "','"); strSQL.Append(textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "','"); strSQL.Append(textBox5.Text.Trim().ToString() + "','" + textBox6.Text.Trim().ToString() + "')");
string strAge = textBox4.Text.Trim(); //提取年龄、性别、邮箱地址、联系电话 string strSex = textBox3.Text.Trim().ToString(); string strEmail = textBox6.Text.Trim().ToString(); string strPhone = textBox5.Text.Trim();
if (ifAge(strAge))//年龄应该大于0 { if (ifSex(strSex)) //性别只能是“男”或“女” { if (strPhone.Length == 11) //手机号必须11位数 正则表达式"(1[0-9]{10}$" { if (ifEmail(strEmail) //邮件地址必须有@ { using (OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), Olecon)) { Olecon.Open(); cmd.ExecuteNonQuery(); MaxValue = Convert.ToInt32(new OleDbCommand("select Count(*) from Student", Olecon).ExecuteScalar()); Olecon.Close(); Olecon.Dispose(); } MessageBox.Show("已成功向数据库表中插入一条记录!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); State = MaxValue - 1; this.textBox1.Enabled = false; this.textBox2.Enabled = false; this.textBox3.Enabled = false; this.textBox4.Enabled = false; this.textBox5.Enabled = false; this.textBox6.Enabled = false; bAdding = false; } else MessageBox.Show("请输入正确的邮箱地址!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("请输入11位手机号!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("请输入正确的性别!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("请输入正确的年龄!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
谢谢阅读,欢迎交流
相关文章推荐
- mysql 相关
- 研究网狐读取数据库字段<二>
- oracle 数据库表结构
- mongodb(4查询)
- oracle sql学习五
- Redis介绍以及安装(Linux)
- SQL 2
- Mac安装配置MongoDB
- mysql5.7密码过期ERROR 1862 (HY000): Your password has expired. To log in you must change
- presto-0.147+postgresql-9.5.3+msql-5.0.7+hadoop-2.5.2+hive-1.2.1环境构筑以及测试
- Redis相关概念
- mysql的基本知识
- Mysql 授权用户进行远程连接
- Redis集群使用总结(一)
- 数据库三范式
- MySQL7:性能优化
- MySQL6:视图
- MySQL5:触发器
- MySQL4:索引
- MySQL3:存储过程和函数