当前位置:编程学习 > C#/ASP.NET >>

C#中数据库的更新和删除记录的两种实现方式

框图在上一篇文章中给出了,删除、更新和插入一样,也都有两种方式,一种是在数据源的编辑器中设定相应的方法来实现,另一种是自己写代码完成。

下面先给出更新的代码:

         test_dbDataSet.courseRow cro;
            test_dbDataSet.facultyRow fro;
            if ( (course.Text == "") || (facultyid.Text == "") || (this.name.Text == "") || (this.num.Text == ""))
                MessageBox.Show("有一项或者多项没有填写内容,请填写完毕后再提交!");
            else
            {

               ///第一种方式      这里更新都没有更新表的主键,如果要更新主键最好方法就是找到该记录,删除它,然后在插入一个新的记录。
                if (cbupdate.Text == "tableAdater.update")
                {

                    int f = facultyTableAdapter.Updatefaculty(this.name.Text, this.num.Text, this.facultyid.Text);
                    int c = courseTableAdapter.Updatecourse(this.course.Text, this.facultyid.Text);
                    if ((f > 0) && (c > 0))
                        MessageBox.Show("更新成功!");
                    else
                        MessageBox.Show("更新失败!");
                }

                ///第二种方式
                else
                {
                    string cid;
                    fro = test_dbDataSet.faculty.FindByfaculty_id(facultyid.Text);
                    fro = updatefacultydata(ref fro);
                    this.Validate();
                    facultyBindingSource1.EndEdit();
                    int f = facultyTableAdapter.Update(test_dbDataSet.faculty);

                   cid = courseTableAdapter.getcourseid_fid(facultyid.Text);
                   cro = test_dbDataSet.course.FindBycourse_id(cid);
                   cro = updatecoursedata(ref cro);

                    ///this.Validate();
                    ///facultyBindingSource1.EndEdit();
                  ///  courseBindingSource1.EndEdit();


                    int c = courseTableAdapter.Update(test_dbDataSet.course);

                   if((f>0)&&(c>0))
                        MessageBox.Show("第二种方式更新成功!");
                    else
                        MessageBox.Show("第二种方式更新失败!");


                }

 

 

删除的代码:

               

           test_dbDataSet.courseRow crow;
            test_dbDataSet.facultyRow frow;

           ///首先会弹出对话框询问是否要删除数据。


            if (MessageBox.Show("您确定要删除数据吗?", "delete", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {

                 ///第一种方式
                if (cbway.Text == "tableAdater.delete")   
                {
                    string str;
                    str = facultyTableAdapter.getid(cbdelete.Text);
                    int k = facultyTableAdapter.Deletefaculty(str);
                    int j = courseTableAdapter.Deletecourse(str);
                    if ((k > 0) && (j > 0))
                        MessageBox.Show("删除成功!");
                    else
     &

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,