您的位置:首页 > 数据库

[C#] 操作数据库时检验性别、年龄、邮箱、手机号的输入

2016-05-20 18:34 706 查看
我们在添加或编辑数据库记录时,毫无疑问,都需要检验各个输入的格式是否满足要求。

这里首先列出判断性别、年龄、邮箱、手机号输入是否符合规范的函数,其中引出正则表达式这个知识点,最后举出实际添加记录的例子。

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);
}


谢谢阅读,欢迎交流
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: