Presentation is loading. Please wait.

Presentation is loading. Please wait.

Command Object’s ExecuteNonQuery Method ISYS 512.

Similar presentations


Presentation on theme: "Command Object’s ExecuteNonQuery Method ISYS 512."— Presentation transcript:

1 Command Object’s ExecuteNonQuery Method ISYS 512

2 Using ExecuteNonQuery Method To run SQL: – Insert – Delete – Update The ExecuteNonQuery method also returns a value indicating the number of records affected by the SQL statement.

3 Use ExecuteNonQuery to Insert A New Record Create unbound text boxes to enter new customer record. string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQLInsert; strSQLInsert = "Insert into Customer values ('"; strSQLInsert += textBox1.Text + "','" + textBox2.Text + "','"; strSQLInsert += textBox3.Text + "','" + textBox4.Text + "')"; SqlCommand objCommInsert = new SqlCommand(strSQLInsert, objConn); objConn.Open(); MessageBox.Show(strSQLInsert.ToString()); try { objCommInsert.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } objConn.Close();

4 Use ExecuteNonQuery to Delete A Record 1.Create a listbox with CIDs 2.Delete the selected record and remove the CID from the listbox. string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "delete from customer where cid = '" + listBox1.SelectedItem + "'"; SqlCommand objComm = new SqlCommand(strSQL, objConn); try { int affectedRecords; objConn.Open(); affectedRecords= objComm.ExecuteNonQuery(); MessageBox.Show(affectedRecords.ToString() + "records deleted"); listBox1.Items.RemoveAt(listBox1.SelectedIndex); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } objConn.Close();

5 Use ExecuteNonQuery to Update A New Record Create a project that do the following tasks: – Use a DataReader to retrieve customer IDs and populate a listbox. – Select a new rating from radio buttons for the selected customer. – Update customer’s rating using the ExecuteNonQuery method of a Command object.

6 create CID listbox: private void Form4_Load(object sender, EventArgs e) { try { string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); objConn.Open(); string strSQL = "select * from customer;"; SqlCommand objComm = new SqlCommand(strSQL, objConn); SqlDataReader objDataReader; objDataReader = objComm.ExecuteReader(); while (objDataReader.Read() == true) { listBox1.Items.Add(objDataReader["cid"]); // listBox1.Items.Add(objDataReader.GetString(0)); } objConn.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); }

7 Update customer rating: private void button1_Click(object sender, EventArgs e) { string newRating; if (radioButton1.Checked) { newRating = "A"; } else if (radioButton2.Checked) { newRating = "B"; } else { newRating = "C"; } string strConn = "Data Source=(localdb)\\projects;Initial Catalog=SalesDB;Integrated Security=True"; SqlConnection objConn = new SqlConnection(strConn); string strSQL = "Update customer set rating = '" + newRating + "' where cid='" + listBox1.SelectedItem + "'"; SqlCommand objComm = new SqlCommand(strSQL, objConn); try { objConn.Open(); objComm.ExecuteNonQuery(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } objConn.Close(); }

8 Possible Database Errors SQL syntax errors Database connection not open Null Violate database constraints Referential integrity Field data type and length Primary key constraint Invalid data – database may have validation rules.


Download ppt "Command Object’s ExecuteNonQuery Method ISYS 512."

Similar presentations


Ads by Google