Asp. NET uses dynamic construction statement to realize efficient batch deletion

Source: Internet
Author: User
Tags add key sql net

Bulk deletion in the Web site is common, especially for bulk deletions in the GridView. Our general practice is to loop to the tick to delete the call procedure directly. In this way, individuals feel that the efficiency is not very high, if hundreds of thousands of data to be deleted, it will have to invoke hundreds of trials. In fact, we can use dynamic build DELETE statements for efficient bulk deletion, no matter how much you choose to delete the data, just call the trial once.

The stored procedure deletion statement below has nothing to do with the above diagram, which is common in the GridView for bulk deletion. Using Stored procedures:

 
 
  1. ---------------------------------------------------------------------------------------
  2. * Author: Lin Sen * Function Description: Dynamic construction of the deletion of SQL statements * written Date: September 27, 2010 **/
  3. ---------------------------------------------------------------------------------------
  4. drop procedure Proc_deletemessagegocreate Procedure Proc_deletemessage (
  5. @condition
  6. VARCHAR (500)
  7. --Delete criteria (multiple)
  8. ) Asbegin
  9. DECLARE @sql varchar (200)
  10. --Dynamically building DELETE statements
  11. Select @sql = "Delete from Messageinfo where" + @condition
  12. --Trial Statement exec (@sql) Endgo

This procedure is called on SQL Query Analyzer: (the criteria passed in is uniquely identifying the column name and the selected value)

 
  
  
  1. EXEC proc_deletemessage "messageid=240 or messageid=241 or messageid=242 ..."

In the SQL call is not very clear, we look at the web in front of the call and trial.

 
 
  1. Delete button click event
  2. protected void Lbtn_del_click (object sender, EventArgs e)
  3. {
  4. StringBuilder sb = new StringBuilder ();
  5. for (int i = 0; i < Gv_class. Rows.Count; i++)
  6. {
  7. CheckBox checkbox = (checkbox) Gv_class. Rows[i]. FindControl ("checkbox");
  8. if (checkbox. Checked = = False)
  9. {
  10. Lab_note.text = "Please select to delete Information";
  11. LAB_NOTE.STYLE.ADD ("Color", "red");
  12. }
  13. Else
  14. {
  15. messagemodel.c_id = Int32.Parse (gv_class. Rows[i]. CELLS[3]. Text.trim ());//Selected unique identity column value
  16. Sb. Append ("messageid=");
  17. Sb. Append (messagemodel.c_id);
  18. Sb. Append ("or");
  19. }
  20. }
  21. Sb. Append ("Messageid=null");
  22. Messagemodel.sqlstr = sb. ToString ();//Dynamic Conditional statement passed to entity
  23. Int J = deleteclass (Messagemodel);
  24. //.....        }
  25. <summary>
  26. Delete Information
  27. </summary>
  28. <param name= "Memodel" ></param>
  29. <returns></returns>
  30. public int deleteclass (Messagemodel memodel)
  31. {
  32. int rowsaffected;
  33. sqlparameter[] parameter = {new SqlParameter ("@sqlstr", SqlDbType.Int)};
  34. Parameter[0]. Value = Memodel.sqlstr;
  35. Dbhelpersql.runintprocname ("Proc_deletemessage", out rowsaffected, parameter);
  36. return rowsaffected;
  37. }

When the Web foreground dynamically builds and invokes procedures, we need to note a few things:

1, we check the column is generally a primary key unique identity column, according to it to delete.

2, if your primary key is a string is not plastic, then the above need to change sb. Append ("messageid="); sb. Append (messagemodel.c_id);

Sb. Append ("" or "), just multiple single quotes.

3, after the cycle after the attention plus SB. Append ("Messageid=null"); The individual believes that the primary key cannot be empty, so this condition is excluded. A dynamic statement that is not added is followed by a lot of or;

But do not add or 1=1 in the condition, so no matter how many are set up, will be the entire data deleted.

4, the construction of the statement with parameters passed in, as a deletion condition.

If there are other methods of efficient batch data deletion, I hope to post it and discuss and share with you.

Original link: http://www.cnblogs.com/lsyfg/archive/2010/12/28/1919186.html

"Edit Recommendation"



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.