/// <summary> /// 增加一条数据 /// </summary> public bool Add(Maticsoft.Model.Students model) { StringBuilder strSql = new StringBuilder(); StringBuilder strSql1 = new StringBuilder(); StringBuilder strSql2 = new StringBuilder(); if (model.ID != null) { strSql1.Append("ID,"); strSql2.Append("'" + model.ID + "',"); } if (model.stuName != null) { strSql1.Append("stuName,"); strSql2.Append("'" + model.stuName + "',"); } if (model.stuSex >= 0 && model.stuSex <= 1) { strSql1.Append("stuSex,"); strSql2.Append("" + model.stuSex + ","); } if (model.stuCollege != null) { strSql1.Append("stuCollege,"); strSql2.Append("'" + model.stuCollege + "',"); } if (model.stuMajor != null) { strSql1.Append("stuMajor,"); strSql2.Append("'" + model.stuMajor + "',"); } if (model.stuClass != null) { strSql1.Append("stuClass,"); strSql2.Append("'" + model.stuClass + "',"); } if (model.passwd != null) { strSql1.Append("passwd,"); strSql2.Append("'" + model.passwd + "',"); } strSql.Append("insert into Students("); strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1)); strSql.Append(")"); strSql.Append(" values ("); strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1)); strSql.Append(")"); int rows = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rows > 0) { return(true); } else { return(false); } }
private void btConfirm_Click(object sender, EventArgs e) { string ID = tbID.Text.Trim(), stuName = tbStuName.Text.Trim(), stuCollege = tbStuCollege.Text.Trim(), stuMajor = tbStuMajor.Text.Trim(), stuClass = tbStuClass.Text.Trim(), passwd = tbPasswd.Text.Trim(), confirmPasswd = tbConfirmPasswd.Text.Trim(); int stuSex = rbStuSex1.Checked ? 1 : 0; if (stuName == "" || stuName == null || stuCollege == "" || stuCollege == null || stuMajor == "" || stuMajor == null || stuClass == "" || stuClass == null || passwd == "" || passwd == null || confirmPasswd == "" || confirmPasswd == null) { MessageBox.Show("不能有空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (passwd.Equals(confirmPasswd)) { if (new Maticsoft.BLL.Students().Exists(ID)) { MessageBox.Show("学号已存在!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { Maticsoft.Model.Students student = new Maticsoft.Model.Students(); student.ID = ID; student.stuName = stuName; student.stuSex = stuSex; student.stuCollege = stuCollege; student.stuClass = stuClass; student.stuMajor = stuMajor; student.passwd = passwd; Maticsoft.Common.StaticDataClass.student = student; Main main = (Main)this.Owner; if (main.AddStudent()) { MessageBox.Show("添加成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); main.LoadData_Student(); this.Close(); } else { MessageBox.Show("添加失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("两次密码不一致!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Maticsoft.Model.Students model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Students set "); if (model.stuName != null) { strSql.Append("stuName='" + model.stuName + "',"); } if (model.stuSex >= 0 && model.stuSex <= 1) { strSql.Append("stuSex=" + model.stuSex + ","); } if (model.stuCollege != null) { strSql.Append("stuCollege='" + model.stuCollege + "',"); } if (model.stuMajor != null) { strSql.Append("stuMajor='" + model.stuMajor + "',"); } if (model.stuClass != null) { strSql.Append("stuClass='" + model.stuClass + "',"); } if (model.passwd != null) { strSql.Append("passwd='" + model.passwd + "',"); } int n = strSql.ToString().LastIndexOf(","); strSql.Remove(n, 1); strSql.Append(" where ID='" + model.ID + "' "); int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Maticsoft.Model.Students GetModel(string ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 "); strSql.Append(" ID,stuName,(case stuSex when 1 then '男' when 0 then '女' else '未知' end)stuSex,stuCollege,stuMajor,stuClass,passwd "); strSql.Append(" from Students "); strSql.Append(" where ID='" + ID + "' "); Maticsoft.Model.Students model = new Maticsoft.Model.Students(); DataSet ds = DbHelperSQL.Query(strSql.ToString()); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Maticsoft.Model.Students DataRowToModel(DataRow row) { Maticsoft.Model.Students model = new Maticsoft.Model.Students(); if (row != null) { if (row["ID"] != null) { model.ID = row["ID"].ToString(); } if (row["stuName"] != null) { model.stuName = row["stuName"].ToString(); } if (row["stuSex"] != null && row["stuSex"].ToString() != "") { model.stuSex = int.Parse(row["stuSex"].ToString()); } if (row["stuCollege"] != null) { model.stuCollege = row["stuCollege"].ToString(); } if (row["stuMajor"] != null) { model.stuMajor = row["stuMajor"].ToString(); } if (row["stuClass"] != null) { model.stuClass = row["stuClass"].ToString(); } if (row["passwd"] != null) { model.passwd = row["passwd"].ToString(); } } return(model); }