Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is their a way I canget the total number of records affected by my update query?

ID: 3770458 • Letter: I

Question

Is their a way I canget the total number of records affected by my update query? I know i can see it if I run the sql statement in the database itself, but can i retrieve that number and put it into the message box showing the data has been updated?

private void button2_Click(object sender, EventArgs e)

{

    try

    {

        //This is my connection string i have assigned the database file address path

        string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";

        //This is my update query in which i am taking input from the user through windows forms and update the record.

        string Query = "update student.studentinfo set idStudentInfo='" + this.IdTextBox.Text + "',Name='" + this.NameTextBox.Text + "',Father_Name='" + this.FnameTextBox.Text + "',Age='" + this.AgeTextBox.Text + "',Semester='" + this.SemesterTextBox.Text + "' where idStudentInfo='" + this.IdTextBox.Text + "';";

        //This is MySqlConnection here i have created the object and pass my connection string.

        MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);

        MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);

        MySqlDataReader MyReader2;

        MyConn2.Open();

        MyReader2 = MyCommand2.ExecuteReader();

        MessageBox.Show("Data Updated");

        while (MyReader2.Read())

        {

        }

        MyConn2.Close();//Connection closed here

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message);

    }

}

Explanation / Answer

private void button2_Click(object sender, EventArgs e)

{

try

{

//This is my connection string i have assigned the database file address path

string MyConnection2 = "datasource=localhost;port=3307;username=root;password=root";

//This is my update query in which i am taking input from the user through windows forms and update the record.

string Query = "update student.studentinfo set idStudentInfo='" + this.IdTextBox.Text + "',Name='" + this.NameTextBox.Text + "',Father_Name='" + this.FnameTextBox.Text + "',Age='" + this.AgeTextBox.Text + "',Semester='" + this.SemesterTextBox.Text + "' where idStudentInfo='" + this.IdTextBox.Text + "';";

//This is MySqlConnection here i have created the object and pass my connection string.

MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);

MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);

int count = MyCommand2.ExecuteNonQuery();

MessageBox.Show("Data Updated" + count);



}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote