Ejemplo n.º 1
0
        /// <summary>
        /// 新建一个学生用户,如果表中有stu_statu为0的数据,
        /// 则将该学号给这个学生,保证不断号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="gender"></param>
        /// <param name="birth"></param>
        /// <param name="native"></param>
        public void CreateUser(string name, string genderString, string shortDateString, string native)
        {
            string code    = "";                                    //学生编号
            string psw     = CommonFunction.MD5Encrypt32("aaaaaa"); //默认密码aaaaaa,加密存储
            char   gender  = (genderString.Equals("男") ? '0' : '1');
            string birth   = CommonFunction.DateString8(shortDateString);
            char   statu   = '1';
            char   isAdmin = '0';
            string type    = "";
            //string tmp = ",";

            //先查找出最小的被删除的学生号码,被删除statu会被置为0,
            string sqlMinNonCode = "select min(convert(int,STU_CODE)) from TD_Student where STU_STATU='0'";

            //所有学生的statu值都为1时,说明没有断号,则找到最大的号码
            string sqlMaxCode = "select max(convert(int,STU_CODE)) from TD_Student";

            //无论表里有没有数据,resultList里都有数据
            //有数据时resultList[0]为第一条数据,以此类推
            //无数据时resultList[0]=="",即空字符串
            List <List <string> > resultList = CommonFunction.ExecuteSqlReader(sqlMinNonCode);

            if (resultList[0][0] != "")//有断号
            {
                //从表里挑出的最小被删除学生编号,是int类型,需要转换成"0000"格式的字符串
                code = Convert.ToInt32(resultList[0][0]).ToString("0000");
                type = "update";
                userModel.SaveUser(code, name, psw, gender, birth, native, statu, isAdmin, type);
            }
            else//无断号,resultList[0][0]=="";
            {
                resultList = CommonFunction.ExecuteSqlReader(sqlMaxCode);
                if (resultList[0][0] != "")//表里有数据,已存在学生
                {
                    code = (Convert.ToInt32(resultList[0][0].ToString()) + 1).ToString("0000");
                    type = "insert";
                    userModel.SaveUser(code, name, psw, gender, birth, native, statu, isAdmin, type);
                }
                else//表里无数据,resultList[0][0]=="";
                {
                    code = "0001";
                    type = "insert";
                    userModel.SaveUser(code, name, psw, gender, birth, native, statu, isAdmin, type);
                }
            }
        }