基于OOP实现对象添加到数据库

本文详细介绍了如何使用面向对象编程(OOP)原则在数据库中添加对象实例,通过实例化学生对象并将其属性映射到数据库字段,展示了数据访问对象(DAO)模式的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于OOP实现对象添加到数据库

		 //初始化内容应该写在构造方法里面
        //定义一个数据层访问对象

        private StudentService objStudentService = new StudentService();

    //封装学员对象
            Student objStudent = new Student()
            {
                StudentName = this.txtStudentName.Text.Trim(),
                Gender =this.rdoFemale.Checked?"男":"女",
                Birthday = Convert.ToDateTime(this.dtpBirthday.Text),
                Age = DateTime.Now.Year - Convert.ToDateTime(this.dtpBirthday.Text).Year,
                ClassId = Convert.ToInt32(this.cboClassName.SelectedValue),
                StudentIdNo = this.txtStudentIdNo.Text.Trim(),
                CardNo = this.txtCardNo.Text.Trim(),
                PhoneNumber = this.txtPhoneNumber.Text.Trim(),
                StudentAddress = this.txtPhoneNumber.Text.Trim(),
                StuImage = this.pbStu.Image ==null?"":new SerializeObjectToString().SerializeObject(this.pbStu.Image),

            };

            //提交对象
            try
            {
                int result = objStudentService.AddStudent(objStudent);
                if (result == 1)
                {
                    DialogResult dresult = MessageBox.Show("添加成功,继续添加么", "添加询问", MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                    if (dresult == DialogResult.OK)
                    {
                        //清空当前文本框
                        foreach (Control item in this.groupBox1.Controls)
                        {
                            if (item is TextBox)
                                item.Text = "";
                            else if (item is RadioButton)
                                ((RadioButton)item).Checked = false;
                            else if (item is ComboBox)
                                ((ComboBox)item).SelectedIndex = -1;
                        }
                        this.pbStu.Image = null;
                        this.txtStudentName.Focus();
                    }                        
                }
                else MessageBox.Show("添加失败", "添加提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            

            //判断是否保存成功
        }
 /// <summary>
        /// 添加学员对象
        /// </summary>
        /// <param name="objStudent"></param>
        /// <returns></returns>
        public int AddStudent(Student objStudent)
        {
            string sql = "insert into Students (StudentName,Gender,Birthday,Age,ClassId,StudentIdNo,CardNo,PhoneNumber,StudentAddress,StuImage) ";
            sql += "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')";
            sql = string.Format(sql, objStudent.StudentName,objStudent.Gender,objStudent.Birthday, objStudent.Age, 
                objStudent.ClassId, objStudent.StudentIdNo,objStudent.CardNo, objStudent.PhoneNumber, 
                objStudent.StudentAddress, objStudent.StuImage);
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (Exception ex)
            {
                throw new Exception("保存数据出现问题!"+ex.Message);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值