C# 根据用户性别,在身份证号输入框后 面显示问候信息,格式为:“Mrs./Mr. Name”。

当用户输入正确的身份证号后,根据用户性别,在身份证号输入框后面显示问候信息,格式为:“Mrs./Mr. Name”。

 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = DB.Select("select Idnumber,Name,Gender from [User] where Idnumber='" + textBox1.Text + "'");
            if (dt.Rows.Count > 0)
            {
                DataRow theRow = dt.Rows[0];
                switch (theRow["Gender"].ToString())//查找性别的字段
                {
                    case "M"://根据数据库存放的字段而定
                        lb_sex.Text = "Mr." + theRow["Name"];
                        break;
                    case "F":
                        lb_sex.Text = "Mrs." + theRow["Name"];
                        break;
                }
                
            }
          }

这是在文本框的文字改变事件里面

至于这个DB是什么,在上一次讲过了,是单独的一个类,我再贴一次

 public static DataTable Select(string sql)
        {
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection("server=localhost;database=数据库名称;user id=sa;pwd=密码"))
            {
                SqlDataAdapter data = new SqlDataAdapter(sql, conn);
                data.Fill(dt);
            }
            return dt;
        }

这样就可以了。内容到这里就结束啦!

猜你喜欢

转载自www.cnblogs.com/madan01/p/10642018.html
今日推荐