/// <summary> /// 得到一个对象实体 /// </summary> public Maticsoft.Model.Teachers DataRowToModel(DataRow row) { Maticsoft.Model.Teachers model = new Maticsoft.Model.Teachers(); if (row != null) { if (row["ID"] != null) { model.ID = row["ID"].ToString(); } if (row["teaName"] != null) { model.teaName = row["teaName"].ToString(); } if (row["teaSex"] != null && row["teaSex"].ToString() != "") { model.teaSex = int.Parse(row["teaSex"].ToString()); } if (row["teaTitle"] != null) { model.teaTitle = row["teaTitle"].ToString(); } if (row["passwd"] != null) { model.passwd = row["passwd"].ToString(); } } return(model); }
private void btConfirm_Click(object sender, EventArgs e) { string ID = tbID.Text.Trim(), teaName = tbTeaName.Text.Trim(), teaTitle = tbTeaTitle.Text.Trim(), passwd = tbPasswd.Text.Trim(), confirmPasswd = tbConfirmPasswd.Text.Trim(); int teaSex = rbTeaSex1.Checked ? 1 : 0; if (ID == "" || ID == null || teaName == "" || teaName == null || teaTitle == "" || teaTitle == null || passwd == "" || passwd == null || confirmPasswd == "" || confirmPasswd == null) { MessageBox.Show("不能有空!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (passwd.Equals(confirmPasswd)) { if (new Maticsoft.BLL.Teachers().Exists(ID)) { MessageBox.Show("教工号已存在!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { Maticsoft.Model.Teachers teacher = new Maticsoft.Model.Teachers(); teacher.ID = ID; teacher.teaName = teaName; teacher.teaSex = teaSex; teacher.teaTitle = teaTitle; teacher.passwd = passwd; Maticsoft.Common.StaticDataClass.teacher = teacher; Main main = (Main)this.Owner; if (main.AddTeacher()) { MessageBox.Show("添加成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); main.LoadData_Teacher(); this.Close(); } else { MessageBox.Show("添加失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("两次密码不一致!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Maticsoft.Model.Teachers 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.teaName != null) { strSql1.Append("teaName,"); strSql2.Append("'" + model.teaName + "',"); } if (model.teaSex >= 0 && model.teaSex <= 1) { strSql1.Append("teaSex,"); strSql2.Append("" + model.teaSex + ","); } if (model.teaTitle != null) { strSql1.Append("teaTitle,"); strSql2.Append("'" + model.teaTitle + "',"); } if (model.passwd != null) { strSql1.Append("passwd,"); strSql2.Append("'" + model.passwd + "',"); } strSql.Append("insert into Teachers("); 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); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Maticsoft.Model.Teachers GetModel(string ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 "); strSql.Append(" ID,teaName,(case teaSex when 1 then '男' when 0 then '女' else '未知' end)teaSex,teaTitle,passwd "); strSql.Append(" from Teachers "); strSql.Append(" where ID='" + ID + "' "); Maticsoft.Model.Teachers model = new Maticsoft.Model.Teachers(); 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 bool Update(Maticsoft.Model.Teachers model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Teachers set "); if (model.teaName != null) { strSql.Append("teaName='" + model.teaName + "',"); } if (model.teaSex >= 0 && model.teaSex <= 1) { strSql.Append("teaSex=" + model.teaSex + ","); } if (model.teaTitle != null) { strSql.Append("teaTitle='" + model.teaTitle + "',"); } 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); } }