winfrom身份证验证是否成年求大神解答

第一学年项目在网上复制的代码不会用求大佬解答!

if (txtSfz.Text.Length>=18)
            {
                string state = "";
                string num = txtSfz.Text;
                if (num != "")
                {
                    string d = "";
                    TimeSpan ts = new TimeSpan();
                    DateTime dt = new DateTime();
                    if (num.Length == 18)
                    {
                        d = num.Substring(6, 8);

                        dt = DateTime.ParseExact(d, "yyyyMMdd", null);

                    }
                    ts = DateTime.Now.Subtract(dt);
                    DateTime now = DateTime.Now;
                    if ((now.Year - dt.Year) > 18)
                    {
                        state = "已成年,未纳入防沉迷系统";
                        frmZhuYe f = new frmZhuYe();
                        f.Tag = Id;
                        string lj = "server=.;database=XiangMu;uid=sa;pwd=ok";
                        SqlConnection tz = new SqlConnection(lj);
                        try
                        {
                            tz.Open();
                            string ID = Id;
                            string ml = @"update Computer
                            set OnlineStatus = '1'
                            where ComputerCode = '" + ID + "'";
                            SqlCommand zx = new SqlCommand(ml, tz);
                            int jg = zx.ExecuteNonQuery();
                            if (jg > 0)
                            {
                                zx.CommandText = @"insert Sfz values('" + txtSfz.Text + "')";
                                int Sfz = zx.ExecuteNonQuery();
                                if (Sfz > 0)
                                {
                                    MessageBox.Show("开机成功!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            tz.Close();
                        }
                    }
                    if (now.Month >= dt.Month && now.Day >= dt.Day)
                    {
                        state = "已成年,未纳入防沉迷系统";
                        MessageBox.Show(state);
                    }
                    else
                    {
                        state = "未成年,纳入防沉迷系统";
                        MessageBox.Show(state);
                    }

                    //state = ts.ToString();
                }
                return;
            }
            else
            {
                MessageBox.Show("身份证不足十八位!请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

这个验证成年的代码是有问题的 

经过修改后

if (txtSfz.Text.Length >= 18)
            {
                string state = "";
                string num = txtSfz.Text;
                if (num != "")
                {
                    string d = "";
                    TimeSpan ts = new TimeSpan();
                    DateTime dt = new DateTime();
                    if (num.Length == 18)
                    {
                        d = num.Substring(6, 8);

                        dt = DateTime.ParseExact(d, "yyyyMMdd", null);

                    }
                    ts = DateTime.Now.Subtract(dt);
                    DateTime now = DateTime.Now;
                    /*
                     对生日信息有几种情况
                     1.当前年-出生年 > 18 成年
                     2.当前年-出生年 = 18 继续判断月和日 
                       2.1 当前月 < 出生月 未成年
                       2.2 当前月 > 出生月 成年
                       2.3 当前月 = 出生月 比较日
                           2.3.1 当前日 = 出生日 成年
                           2.3.2 当前日 > 出生日 成年
                           2.3.3 当前日 < 出生日 未成年
                     3.当前年-出生年 < 18 未成年   
                     */
                    if ((now.Year - dt.Year) > 18)
                    {
                        state = "已成年,未纳入防沉迷系统";
                        MessageBox.Show(state);
                    }
                   else if ((now.Year - dt.Year) == 18 && now.Month >= dt.Month)
                    {
                        if (now.Month == dt.Month)
                        {
                            if (now.Day >= dt.Day)
                            {
                                state = "已成年,未纳入防沉迷系统";
                            }
                            else
                            {
                                state = "未成年,纳入防沉迷系统";
                            }
                        }
                        else
                        {
                            state = "已成年,未纳入防沉迷系统";
                        }
                        MessageBox.Show(state);
                    }
                    else
                    {
                        state = "未成年,纳入防沉迷系统";
                        MessageBox.Show(state);
                    }

                    //state = ts.ToString();
                }
                return;
            }
            else
            {
                MessageBox.Show("身份证不足十八位!请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

我把有关数据库连接、执行、自定义类等内容剔除了,

只保留了逻辑语句,题主可以根据逻辑语句对代码进行调整

如果回答帮您解决了问题还请题主能给个采纳,您的采纳对我们非常重要

这个代码还设计到数据库操作。数据库有拷贝到本机吗?是否安装了mssqlserver?安装过sa的密码还需要设置为ok才行

里面还存在frmZhuYe这类,不知道你代码是否拷贝完整,帮助到你可以点采纳哦~