C # Two implementation methods for database insertion

Source: Internet
Author: User


Is the form corresponding to the following code, including subsequent updates and inserts.

Test_dbDataSet.courseRow cnewrow; // the course and faculty here are the names of the two data tables under test_dbDataSet.

Test_dbDataSet.facultyRow fnewrow;

If (courseid. text = "") | (course. text = "") | (facultyid. text = "") | (this. name. text = "") | (this. num. text = "") // the Text box contains the data to be inserted.


MessageBox. Show ("one or more items are not filled in. Please enter them before submitting them! ");


Else
{

/// The first method is to create an insert method for each of the two tables in the data editor of the current data source, and then call the two methods respectively to implement the insert, the following insertfaculty and insertcourse parameters are the values to be inserted.


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 ("data update successful! ");
 
Facultyid. Text = "";
This. insert. Enabled = false;

}
Else
{
MessageBox. Show ("data update failed! ");

}
Course. Text = "";
Courseid. Text = "";
This. name. Text = "";
This. num. Text = "";

Courseid. Focus ();
}

 

/// The second method is to create a new row for both tables, insert data into the new row, add the new row to the corresponding data table, and finally update the database.
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); // Update the database

 



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 ("the second method successfully inserts data! ");
This. facultyid. Text = "";
Insert. Enabled = false;
}
Else
{
MessageBox. Show ("the second method failed to insert data! ");

}
Course. Text = "";
Courseid. Text = "";
This. name. Text = "";
This. num. Text = "";
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.