程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#數據庫插入的兩種實現方式

C#數據庫插入的兩種實現方式

編輯:C#入門知識


   上圖就是以下代碼所對應的窗體,包括後面的更新和插入。

         test_dbDataSet.courseRow    cnewrow;      ///     這裡的course和faculty是test_dbDataSet下面的兩個數據表的名稱

            test_dbDataSet.facultyRow   fnewrow;

            if ((courseid.Text == "") || (course.Text == "") || (facultyid.Text == "") || (this.name.Text == "") || (this.num.Text == ""))      /// 這些文本框裡放的是待插入的數據


                MessageBox.Show("有一項或者多項沒有填寫內容,請填寫完畢後再提交!");


            else
            {

               /// 第一種方式是在現在數據源在數據編輯器裡面對兩個表都各自新建一個插入的方法,然後分別調用這兩個方法即可實現插入,如下的insertfaculty和insertcourse,後面的參數就是要插入的值。


                if (cb.Text == "tableAdapter.insert")                  
                {
                    int check = facultyTableAdapter.Insertfaculty(this.facultyid.Text, this.name.Text, this.num.Text);
                    int check1 = courseTableAdapter.Insertcourse(this.courseid.Text, this.course.Text, this.facultyid.Text);
                    if ((check > 0) && (check1 > 0))
                    {
                        MessageBox.Show("數據更新成功!");
 
                        facultyid.Text = "";
                        this.insert.Enabled = false;

                    }
                    else
                    {
                        MessageBox.Show("數據更新失敗!");
                      
                    }
                    course.Text = "";
                    courseid.Text = "";
                    this.name.Text = "";
                    this.num.Text = "";

                    courseid.Focus();
                }

 

          /// 第二種方法是對兩個表都新建一個該表的新行,然後在新行中插入數據,再將該新行加到相應的數據表,最後更新數據庫。
                else
                {
                    fnewrow = test_dbDataSet.faculty.NewfacultyRow();


                    fnewrow.faculty_id = this.facultyid.Text;


                    fnewrow.name = this.name.Text;


                    fnewrow.num = this.num.Text;


                    test_dbDataSet.faculty.Rows.Add(fnewrow);


                    int i = facultyTableAdapter.Update(test_dbDataSet.faculty);    ///更新數據庫

 


                   
                    cnewrow = test_dbDataSet.course.NewcourseRow();


                    cnewrow.course_id = courseid.Text;


                    cnewrow.course = this.course.Text;


                    cnewrow.faculty_id = this.facultyid.Text;


                    test_dbDataSet.course.Rows.Add(cnewrow);


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


                    if ((t > 0) && (i > 0))
                    {
                        MessageBox.Show("第二種方法成功插入數據!");
                        this.facultyid.Text = "";
                        insert.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("第二種方式插入數據失敗!");
                       
                    }
                    course.Text = "";
                    courseid.Text = "";
                    this.name.Text = "";
                    this.num.Text = "";
                }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved