コード例 #1
0
ファイル: index.aspx.cs プロジェクト: zhangbin28/DbClass2016
 public bool check_Num(string ss)
 {
     if (ss.Length == 0)
     {
         labelMsg.Text = "";
         return(true);
     }
     if (ss.Length != 8)
     {
         return(false);
     }
     foreach (char s in ss)
     {
         if (!Char.IsNumber(s))
         {
             return(false);
         }
     }
     if (!CheckSQL.IsSafeSqlString(ss))
     {
         return(false);
     }
     labelMsg.Text = "";
     return(true);
 }
コード例 #2
0
        protected void insertMsg()
        {
            initLab();
            string num = tbox0.Text;

            if (num.Length != 8 || !CheckSQL.IsSafeSqlString(num))
            {
                lab0.Text   = "学号格式错误";
                labMsg.Text = "插入错误";
                return;
            }
            foreach (char s in num)
            {
                if (!Char.IsNumber(s))
                {
                    lab0.Text   = "学号格式错误";
                    labMsg.Text = "插入错误";
                    return;
                }
            }
            string name, sex, age, profession;

            name = tbox1.Text;
            age  = tbox3.Text;
            if (!CheckSQL.IsSafeSqlString(name))
            {
                lab1.Text   = "学号格式错误";
                labMsg.Text = "插入错误";
                return;
            }
            foreach (char s in age)
            {
                if (!Char.IsNumber(s))
                {
                    lab3.Text   = "年龄格式错误";
                    labMsg.Text = "插入错误";
                    return;
                }
            }
            if (!CheckSQL.IsSafeSqlString(age))
            {
                lab3.Text   = "学号格式错误";
                labMsg.Text = "插入错误";
                return;
            }
            sex        = dlist2.SelectedValue;
            profession = dlist4.SelectedValue;
            MySqlDataReader sdr1;

            sdr1 = getSDR("select NAME,AGE,SEX,PROFESSIONID from student where ID = " + num);
            if (sdr1.Read())
            {
                try
                {
                    if ((name != "" && name != sdr1.GetValue(0).ToString()) || (age != "" && age != sdr1.GetValue(1).ToString()) ||
                        (sex != "0" && sex != sdr1.GetValue(2).ToString()) || (profession != "0" && profession != sdr1.GetValue(3).ToString()))
                    {
                        lab1.Text   = "学号已存在";
                        labMsg.Text = "插入错误";
                        return;
                    }
                }
                catch
                {
                    labMsg.Text = "出错啦^_^";
                    sdr1.Close();
                    return;
                }
                sdr1.Close();
                if (dlist5.SelectedValue != "0")
                {
                    try
                    {
                        sdr1 = getSDR("insert into reward values(" + num + "," + dlist5.SelectedValue + ")");
                    }
                    catch
                    {
                        labMsg.Text = "出错啦^_^";
                        sdr1.Close();
                        return;
                    }
                    sdr1.Close();
                    labMsg.Text = "插入信息成功:</p>" +
                                  "学号:" + num + "</p>" +
                                  "奖励:" + dlist5.SelectedItem.Text + "</p>";
                }
                else
                {
                    labMsg.Text = "你啥也不添加是什么鬼=.=";
                }
            }
            else
            {
                sdr1.Close();
                if (name == "")
                {
                    lab1.Text   = "姓名不能为空";
                    labMsg.Text = "插入错误";
                    return;
                }
                if (sex == "0")
                {
                    lab2.Text   = "性别不能为空";
                    labMsg.Text = "插入错误";
                    return;
                }
                if (age == "")
                {
                    lab3.Text   = "年龄不能为空";
                    labMsg.Text = "插入错误";
                    return;
                }
                if (profession == "0")
                {
                    lab4.Text   = "专业不能为空";
                    labMsg.Text = "插入错误";
                    return;
                }

                byte[] utf8 = Encoding.UTF8.GetBytes(name);
                name = Encoding.UTF8.GetString(utf8);;
                try
                {
                    sdr1 = getSDR("insert into student  values(" + num + ",'" + name + "'," + sex + "," + age + "," + profession + ")");
                }
                catch
                {
                    labMsg.Text = "出错啦^_^";
                    sdr1.Close();
                    return;
                }
                sdr1.Close();
                labMsg.Text = "插入信息成功:</p>" +
                              "学号:" + num + "</p>" +
                              "姓名:" + name + "</p>" +
                              "性别:" + dlist2.SelectedItem.Text + "</p>" +
                              "年龄:" + age + "</p>" +
                              "专业:" + dlist4.SelectedItem.Text + "</p>";
                if (dlist5.SelectedValue != "0")
                {
                    try
                    {
                        sdr1 = getSDR("insert into reward values(" + num + "," + dlist5.SelectedValue + ")");
                    }
                    catch
                    {
                        labMsg.Text = "出错啦^_^";
                        sdr1.Close();
                        return;
                    }
                    sdr1.Close();
                    labMsg.Text += "奖励:" + dlist5.SelectedItem.Text;
                }
            }
        }