/** * 添加用户 */ private void Add_Save_Click(object sender, RoutedEventArgs e) { int price; try { price = Int16.Parse(this.aPrice.Text); } catch (Exception age_e) { Message ms = new Message("错误提示", "数据类型不符!价格必须为数字。"); ms.ShowDialog(); return; } Course course = new Course(); course.CATEGORY = this.aCategory.Text; course.COURSENUMBER = this.aCourseNumber.Text; course.NAME = this.aName.Text; course.DETAILS = this.aDetail.Text; course.TEACHER = this.aTeacher.Text; course.TIME = this.aTime.Text; course.PRICE = price; course.REMARK = this.aRemark.Text; //course.photo = this.currentcourse.photo;//Utils.Utils.BitmapImageToByteArray((BitmapImage)(this.aPhoto.Source)); course.PHOTO = Utils.Utils.BitmapImageToByteArray((BitmapImage)(this.aPhoto.Source)); //course._id = Int16.Parse(this.e_id.Text); try { int res = this.db.insertCourse(course); if (res > 0) { Message msg = new Message("添加课程", "成功添加课程" + course.NAME + "。"); msg.ShowDialog(); this.LoadData(); // 自动跳转 List_Click(sender, e); } else { Message msg = new Message("添加客户", "添加课程" + course.NAME + "失败!"); msg.ShowDialog(); } } catch (Exception insert_Exception) { Message ms = new Message("错误提示", "新增课程数据时出错!未能正确连接到数据文件。"); ms.ShowDialog(); } }
private void Edit_Save_Click(object sender, RoutedEventArgs e) { //course course = this.db.getcourseById(id); Course course = new Course(); float price = 0; try { price = float.Parse(this.ePrice.Text); } catch (Exception price_parse) { Message ms = new Message("错误提示", "数据类型不符!价格必须为数字。"); ms.ShowDialog(); return; } course.CATEGORY = this.eCategory.Text; course.COURSENUMBER = this.eCourseNumber.Text; course.NAME = this.eName.Text; course.DETAILS = this.eDetail.Text; course.TEACHER = this.eTeacher.Text; course.TIME = this.eTime.Text; course.PRICE = price; course.REMARK = this.eRemark.Text; //course.photo = this.currentcourse.photo;//Utils.Utils.BitmapImageToByteArray((BitmapImage)(this.aPhoto.Source)); course.PHOTO = Utils.Utils.BitmapImageToByteArray((BitmapImage)(this.ePhoto.Source)); course._id = Int16.Parse(this.e_id.Text); try { int res = this.db.updateCourse(course); if (res > 0) { Message msg = new Message("修改课程资料", "成功修改课程" + course.NAME + "的资料。"); msg.ShowDialog(); this.LoadData(); // 自动跳转 List_Click(sender, e); } else { Message msg = new Message("修改课程资料", "修改课程" + course.NAME + "的资料失败!"); msg.ShowDialog(); } } catch (Exception updata_Exception) { Message ms = new Message("错误提示", "更新课程数据时出错!未能正确连接到数据文件。"); ms.ShowDialog(); } }
public int insertCourse(Course course) { if (course == null) return 0; SQLiteParameter[] parameters = new SQLiteParameter[9]; string sql = "INSERT INTO COURSE(COURSENUMBER, NAME, CATEGORY, TEACHER, TIME, PRICE, DETAILS, REMARK,PHOTO) VALUES(@mCOURSENUMBER, @mNAME, @mCATEGORY, @mTEACHER, @mTIME, @mPRICE, @mDETAILS, @mREMARK,@mPHOTO)"; //parameters[0]=(new SQLiteParameter("m_id", student.id)); parameters[0] = (new SQLiteParameter("@mCOURSENUMBER", course.COURSENUMBER)); parameters[1] = (new SQLiteParameter("@mNAME", course.NAME)); parameters[2] = (new SQLiteParameter("@mCATEGORY", course.CATEGORY)); parameters[3] = (new SQLiteParameter("@mTEACHER", course.TEACHER)); parameters[4] = (new SQLiteParameter("@mTIME", course.TIME)); parameters[5] = (new SQLiteParameter("@mPRICE", course.PRICE)); parameters[6] = (new SQLiteParameter("@mDETAILS", course.DETAILS)); parameters[7] = (new SQLiteParameter("@mREMARK", course.REMARK)); //parameters[9] = (new SQLiteParameter("mREMARK", course.PHOTO)); SQLiteParameter p = new SQLiteParameter("mPHOTO", DbType.Binary); p.Value = course.PHOTO; parameters[8] = p; return this.ExecuteNonQuery(sql, parameters); }
public int updateCourse(Course course) { if (course == null) return 0; SQLiteParameter[] parameters = new SQLiteParameter[10]; string sql = "UPDATE COURSE SET COURSENUMBER = @mCOURSENUMBER, NAME = @mNAME, CATEGORY = @mCATEGORY, TEACHER = @mTEACHER, TIME = @mTIME, PRICE = @mPRICE, DETAILS = @mDETAILS , REMARK = @mREMARK, PHOTO = @mPHOTO WHERE _id = @m_id"; parameters[0] = (new SQLiteParameter("m_id", course._id)); parameters[1] = (new SQLiteParameter("@mCOURSENUMBER", course.COURSENUMBER)); parameters[2] = (new SQLiteParameter("@mNAME", course.NAME)); parameters[3] = (new SQLiteParameter("@mCATEGORY", course.CATEGORY)); parameters[4] = (new SQLiteParameter("@mTEACHER", course.TEACHER)); parameters[5] = (new SQLiteParameter("@mTIME", course.TIME)); parameters[6] = (new SQLiteParameter("@mPRICE", course.PRICE)); parameters[7] = (new SQLiteParameter("@mDETAILS", course.DETAILS)); parameters[8] = (new SQLiteParameter("@mREMARK", course.REMARK)); //parameters[9] = (new SQLiteParameter("mREMARK", course.PHOTO)); SQLiteParameter p = new SQLiteParameter("mPHOTO", DbType.Binary); p.Value = course.PHOTO; parameters[9] = p; return this.ExecuteNonQuery(sql, parameters); }
//课程操作 public Course getCourseById(int id) { Course course = new Course(); SQLiteCommand cmd = this.connection.CreateCommand(); cmd.CommandText = "SELECT * FROM COURSE WHERE _id = " + id; System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { try { course._id = reader.GetInt16(0); } catch (Exception e) { } try { course.COURSENUMBER = reader.GetString(1); } catch (Exception e) { } try { course.NAME = reader.GetString(2); } catch (Exception e) { } try { course.CATEGORY = reader.GetString(3); } catch (Exception e) { } try { course.TEACHER = reader.GetString(4); } catch (Exception e) { } try { course.TIME = reader.GetString(5); } catch (Exception e) { } try { course.PRICE = reader.GetFloat(6); } catch (Exception e) { } try { course.DETAILS = reader.GetString(7); } catch (Exception e) { } try { course.REMARK = reader.GetString(8); } catch (Exception e) { } try {//course.PHOTO = reader.GetString(9); MemoryStream streamImage = new MemoryStream(reader["PHOTO"] as byte[]); byte[] desBytes = new byte[streamImage.Length]; streamImage.Read(desBytes, 0, desBytes.Length); streamImage.Close(); course.PHOTO = desBytes; streamImage.Close(); // 关闭流 } catch (Exception e) { } } reader.Close(); return course; }