C # database update and delete record implementation methods

Source: Internet
Author: User

As shown in the previous article, there are two ways to delete, update, and insert data. One is to set the corresponding method in the data source editor, the other is to write the code by yourself.

The updated code is provided below:

Test_dbDataSet.courseRow cro;
Test_dbDataSet.facultyRow fro;
If (course. text = "") | (facultyid. text = "") | (this. name. text = "") | (this. num. text = ""))
MessageBox. Show ("one or more items are not filled in. Please enter them before submitting them! ");
Else
{

/// In the first method, the primary key of the table is not updated here. The best way to update the primary key is to find the record, delete it, and insert a new record.
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 ("Update successful! ");
Else
MessageBox. Show ("update failed! ");
}

/// Method 2
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 ("the second method is updated successfully! ");
Else
MessageBox. Show ("the second method failed to update! ");


}

 

 

Deleted code:

Test_dbDataSet.courseRow crow;
Test_dbDataSet.facultyRow frow;

/// A dialog box is displayed to check whether data is to be deleted.


If (MessageBox. Show ("are you sure you want to delete the data? "," Delete ", MessageBoxButtons. YesNo) = System. Windows. Forms. DialogResult. Yes)
{

/// Method 1
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 ("deleted successfully! ");
Else
MessageBox. Show ("deletion failed! ");
}

/// Method 2:
Else
{
String id = facultyTableAdapter. getid (cbdelete. Text );
Frow = test_dbDataSet.faculty.FindByfaculty_id (id );
Frow. Delete ();
Int f = facultyTableAdapter. Update (test_dbDataSet.faculty); // The number of deleted records returned by this function

String cid = courseTableAdapter. getcourseid_fid (id );
Crow = test_dbDataSet.course.FindBycourse_id (cid );
Crow. Delete ();
Int c = courseTableAdapter. Update (test_dbDataSet.course );
If (c> 0) & (f> 0 ))
MessageBox. Show ("the second method is deleted successfully! ");
Else
MessageBox. Show ("the second method failed to be deleted! ");


}
 

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.