public const string mysqlConnection = DBConstant.mysqlConnection;//"User Id=root;Host=115.29.229.134;Database=chinaunion;password=c513324665;charset=utf8"; /// <summary> /// 添加数据 /// </summary> /// <returns></returns> public int Add(Exam entity) { string sql = "INSERT INTO tb_exam (subject,sender,creatTime,type,validateStartTime,validateEndTime,isValidate,toAll,agentType,duration)"; sql = sql + " VALUE (@subject,@sender,@creatTime,@type,@validateStartTime,@validateEndTime,@isValidate,@toAll,@agentType,@duration)"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@subject", entity.subject); command.Parameters.AddWithValue("@sender", entity.sender); command.Parameters.AddWithValue("@creatTime", entity.creatTime); command.Parameters.AddWithValue("@type", entity.type); command.Parameters.AddWithValue("@validateStartTime", entity.validateStartTime); command.Parameters.AddWithValue("@validateEndTime", entity.validateEndTime); command.Parameters.AddWithValue("@isValidate", entity.isValidate); command.Parameters.AddWithValue("@toAll", entity.toAll); command.Parameters.AddWithValue("@agentType", entity.agentType); command.Parameters.AddWithValue("@duration", entity.duration); int i = command.ExecuteNonQuery(); mycn.Close(); mycn.Dispose(); return i; } }
/// <summary> /// 根据主键查询 /// </summary> /// <param name="primaryKey"></param> /// <returns></returns> public Exam GetByName(String subject) { string sql = "SELECT sequence,subject,sender,creatTime,type,validateStartTime,validateEndTime,isValidate,toAll,agentType,duration FROM tb_exam WHERE subject=@subject"; using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); command.Parameters.AddWithValue("@subject", subject); MySqlDataReader reader = command.ExecuteReader(); Exam exam = null; if (reader.Read()) { exam = new Exam(); exam.sequence = reader["sequence"] == DBNull.Value ? null : reader["sequence"].ToString(); exam.subject = reader["subject"] == DBNull.Value ? null : reader["subject"].ToString(); exam.creatTime = reader["creatTime"] == DBNull.Value ? null : reader["creatTime"].ToString(); exam.type = reader["type"] == DBNull.Value ? null : reader["type"].ToString(); exam.validateStartTime = reader["validateStartTime"] == DBNull.Value ? null : reader["validateStartTime"].ToString(); exam.validateEndTime = reader["validateEndTime"] == DBNull.Value ? null : reader["validateEndTime"].ToString(); exam.isValidate = reader["isValidate"] == DBNull.Value ? null : reader["isValidate"].ToString(); exam.toAll = reader["toAll"] == DBNull.Value ? null : reader["toAll"].ToString(); exam.agentType = reader["agentType"] == DBNull.Value ? null : reader["agentType"].ToString(); exam.duration = reader["duration"] == DBNull.Value ? null : reader["duration"].ToString(); } mycn.Close(); return exam; } }
private void btnSave_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.txtExamName.Text.Trim())) { MessageBox.Show("名称不能为空"); txtExamName.Focus(); return; } if (this.dtEndDate.Value.CompareTo(this.dtStartDate.Value) <= 0) { MessageBox.Show("有效期结束时间必须大于开始时间"); return; } this.Cursor = Cursors.WaitCursor; Exam exam = new Exam(); if (rdoExam.Checked) { exam.type = "Exam"; } if (rdoSurvey.Checked) { exam.type = "Survey"; } if (String.IsNullOrEmpty(exam.type)) { MessageBox.Show("请选择试题类型"); return; } exam.subject = this.txtExamName.Text; //exam.type = "Exam"; exam.validateStartTime = this.dtStartDate.Value.ToString("yyyy-MM-dd"); exam.validateEndTime = this.dtEndDate.Value.ToString("yyyy-MM-dd"); exam.creatTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); exam.isValidate = "Y"; exam.agentType = this.cboAgentType.Text; ExamDao examDao = new ExamDao(); examDao.Add(exam); exam = examDao.GetByName(exam.subject); if (exam != null) { ExamQuestionDao examQuestionDao = new ExamQuestionDao(); for (int i = 0; i < this.dgExamSingleChoice.RowCount; i++) { ExamQuestion examQuestion = new ExamQuestion(); examQuestion.question = dgExamSingleChoice[0, i].Value.ToString(); examQuestion.answer = dgExamSingleChoice[1, i].Value.ToString(); examQuestion.option1 = dgExamSingleChoice[2, i].Value.ToString(); examQuestion.option2 = dgExamSingleChoice[3, i].Value.ToString(); examQuestion.option3 = dgExamSingleChoice[4, i].Value.ToString(); examQuestion.option4 = dgExamSingleChoice[5, i].Value.ToString(); examQuestion.option5 = dgExamSingleChoice[6, i].Value.ToString(); examQuestion.option6 = dgExamSingleChoice[7, i].Value.ToString(); examQuestion.option7 = dgExamSingleChoice[8, i].Value.ToString(); // examQuestion.option8 = dgExamSingleChoice[9, i].Value.ToString(); examQuestion.questionType = "Single"; examQuestion.exam_sequence = exam.sequence; examQuestionDao.Add(examQuestion); } for (int i = 0; i < this.dgExamMultiChoice.RowCount; i++) { ExamQuestion examQuestion = new ExamQuestion(); examQuestion.question = dgExamMultiChoice[0, i].Value.ToString(); examQuestion.answer = dgExamMultiChoice[1, i].Value.ToString(); examQuestion.option1 = dgExamMultiChoice[2, i].Value.ToString(); examQuestion.option2 = dgExamMultiChoice[3, i].Value.ToString(); examQuestion.option3 = dgExamMultiChoice[4, i].Value.ToString(); examQuestion.option4 = dgExamMultiChoice[5, i].Value.ToString(); examQuestion.option5 = dgExamMultiChoice[6, i].Value.ToString(); examQuestion.option6 = dgExamMultiChoice[7, i].Value.ToString(); examQuestion.option7 = dgExamMultiChoice[8, i].Value.ToString(); // examQuestion.option8 = dgExamMultiChoice[9, i].Value.ToString(); examQuestion.questionType = "Multi"; examQuestion.exam_sequence = exam.sequence; examQuestionDao.Add(examQuestion); } for (int i = 0; i < this.dgExamJugement.RowCount; i++) { ExamQuestion examQuestion = new ExamQuestion(); examQuestion.question = dgExamJugement[0, i].Value.ToString(); examQuestion.answer = dgExamJugement[1, i].Value.ToString(); examQuestion.exam_sequence = exam.sequence; examQuestion.questionType = "Jugement"; examQuestionDao.Add(examQuestion); } ExamReceiverDao examReceiverDao = new ChinaUnion_DataAccess.ExamReceiverDao(); examReceiverDao.Delete(exam.sequence); for (int i = 0; i < lstAgentType.Items.Count; i++) { if (lstAgentType.GetItemChecked(i)) { ExamReceiver examReceiver = new ExamReceiver(); examReceiver.examSequence = exam.sequence; examReceiver.receiver = lstAgentType.Items[i].ToString(); examReceiver.type = "渠道类型"; examReceiverDao.Add(examReceiver); } } for (int i = 0; i < lstGroup.Items.Count; i++) { if (lstGroup.GetItemChecked(i)) { ExamReceiver examReceiver = new ExamReceiver(); examReceiver.examSequence = exam.sequence; examReceiver.receiver = lstGroup.Items[i].ToString(); examReceiver.type = "自定义组"; examReceiverDao.Add(examReceiver); } } } MessageBox.Show("操作完毕"); this.Cursor = Cursors.Default; }
/// <summary> /// 查询集合 /// </summary> /// <returns></returns> public IList<Exam> GetList(String keyword,String type) { string sql = "SELECT sequence, subject,sender,creatTime,type,validateStartTime,validateEndTime,isValidate,toAll,agentType,duration FROM tb_exam WHERE 1=1"; if (!String.IsNullOrEmpty(keyword)) { sql = sql + " and subject like '%" + keyword+"%'"; } if (!String.IsNullOrEmpty(type)) { sql = sql + " and type = '" + type + "'"; } using (MySqlConnection mycn = new MySqlConnection(mysqlConnection)) { mycn.Open(); MySqlCommand command = new MySqlCommand(sql, mycn); MySqlDataReader reader = command.ExecuteReader(); IList<Exam> list = new List<Exam>(); Exam exam = null; while (reader.Read()) { exam = new Exam(); exam.sequence = reader["sequence"] == DBNull.Value ? null : reader["sequence"].ToString(); exam.subject = reader["subject"] == DBNull.Value ? null : reader["subject"].ToString(); exam.creatTime = reader["creatTime"] == DBNull.Value ? null : reader["creatTime"].ToString(); exam.type = reader["type"] == DBNull.Value ? null : reader["type"].ToString(); exam.validateStartTime = reader["validateStartTime"] == DBNull.Value ? null : reader["validateStartTime"].ToString(); exam.validateEndTime = reader["validateEndTime"] == DBNull.Value ? null : reader["validateEndTime"].ToString(); exam.isValidate = reader["isValidate"] == DBNull.Value ? null : reader["isValidate"].ToString(); exam.toAll = reader["toAll"] == DBNull.Value ? null : reader["toAll"].ToString(); exam.agentType = reader["agentType"] == DBNull.Value ? null : reader["agentType"].ToString(); exam.duration = reader["duration"] == DBNull.Value ? null : reader["duration"].ToString(); list.Add(exam); } mycn.Close(); return list; } }