Asp.net implements batch instance deletion and asp.net batch Deletion

Source: Internet
Author: User

Asp.net implements batch instance deletion and asp.net batch Deletion

This example describes how to implement the batch delete function in asp.net. It has some reference value for learning asp.net. Share it with you for your reference. Specific implementation methods:

The. aspx file code is as follows:

<Asp: gridView ID = "GridView1" runat = "server" Width = "100%" EmptyDataText = "no data for now" BorderColor = "White" OnRowDeleting = "GridView1_RowDeleting"> <Columns> <asp: templateField HeaderText = "select"> <ItemStyle Width = "20px"/> <ItemTemplate> <asp: checkBox id = "id" runat = "Server"/> </ItemTemplate> </asp: TemplateField> <asp: boundField DataField = "id" HeaderText = "no."> <ItemStyle Width = "20px"/> </asp: BoundField> <asp: Te MplateField HeaderText = "title"> <ItemStyle Width = "400px"/> <ItemTemplate> <a href = ".. /shangpu/<% # eval_r ("pageurl") %> "target =" _ blank "> <% # eval_r (" title ") %> </a> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText = "posting time"> <ItemStyle Width = "100px"/> <ItemTemplate> <% # Convert. toDateTime (eval_r ("addtime ")). date. toString ("yyyy-MM-dd") %> </ItemTemplate> </asp: TemplateField> <asp: HyperLin KField DataNavigateUrlFormatString = "shangpu_edit.aspx? Id = {0} "Text =" modify "NavigateUrl =" shangpu_edit.aspx? Id = {0} "DataNavigateUrlFields =" id "> <ItemStyle Width =" 30px "/> </asp: HyperLinkField> <asp: commandField ShowDeleteButton = "True" HeaderText = "delete" DeleteText = "<div id =" de "onclick =" JavaScript: return confirm ('Are you sure you want to delete it? ') "> Delete </div>"> <ItemStyle Width = "30px"/> </asp: commandField> </Columns> <EmptyDataTemplate> <font color = red> no data </font> </EmptyDataTemplate> <RowStyle Height = "20px"/> </asp: gridView>

The. cs file code is as follows:

Protected void btndeleteall_Click (object sender, EventArgs e) {string sqltext = "("; for (int I = 0; I <GridView1.Rows. count; I ++) {CheckBox chb = (CheckBox) GridView1.Rows [I]. findControl ("id"); if (chb. checked) {sqltext = sqltext + GridView1.DataKeys [I]. value. toString () + "," ;}} sqltext = sqltext. substring (0, sqltext. length-1) + ")"; sqltext = "delete from shangpu where id in" + sqltext; string SQL Con = ConfigurationManager. appSettings ["ConnectionString"]. toString (); SqlConnection con = new SqlConnection (sqlcon); con. open (); SqlCommand cmd = new SqlCommand (sqltext, con); try {int count = Convert. toInt32 (cmd. executeNonQuery (); if (count> 0) {viewbind (); MessageBox. show (this, "deleted successfully, deleted" + count + "records! ") ;}} Catch {MessageBox. Show (this," deletion failed! ") ;}Finally {con. Close (); con. Dispose ();}}

If you are interested, you can debug and run this example. If you are interested, you can also improve the Code to improve its functions. I hope the examples in this article will be helpful for your learning in asp.net.


ASPNET batch Deletion

SqlConnection con = createcon ();
Con. Open ();

For (int I = 0; I <this. GridView1.Rows. Count; I ++)
{
If (CheckBox) this. GridView1.Rows [I]. FindControl ("CheckBox1"). Checked = true)
{
String pid = this. GridView1.DataKeys [I]. Value;
SqlCommand cmd = new SqlCommand ("delete from province where pid = '" + pid + "'", con );

Cmd. ExecuteNonQuery ();
}
}

Con. Close ();

This. bind ();
Zhang zhanggang [authoritative expert]

How does ASPNET call a stored procedure to delete data in batches?

1. DBHelper

Public int RunSql (string procName, SqlParameter [] sp)
{
Int rowCount = 0;
Try
{
// Obtain the database connection
Conn = getConn ();
// Open the database connection
Conn. Open ();
// Set the Stored Procedure
Cmd = new SqlCommand ();
Cmd. CommandType = CommandType. StoredProcedure;
// Set the stored procedure name
Cmd. CommandText = procName;
// Set the database connection instance
Cmd. Connection = conn;
// Traverse the set of stored procedure parameters
Foreach (SqlParameter parm in sp)
{
// Add stored procedure parameters to the command object parameter set
Cmd. Parameters. Add (parm );
}
// Execute the SQL statement
RowCount = cmd. ExecuteNonQuery ();
Return rowCount;
}
Catch (Exception)
{

Throw;
}
Finally
{
Conn. Close ();
}
}
2. Call Method

Public int deleteById (int id)
{
Int flag = 0;
Try
{
// Create a parameter set
Sp = new SqlParameter [] {
New SqlParameter ("@ id", id) // @ id is the parameter name of the stored procedure.
};
Flag = sqlhelper. RunSql ("proc_Name", sp );
Return flag;
}
Catch (Exception)
{
... The remaining full text>
 

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.