Tuesday, February 14, 2012

Delete data from Sql Server database using ASP.Net

Delete data from Sql Server 2005 Database
Before dalete:-
 

After Delete:-
 
Step 1) Install and configure Sqlserver 2005
Step 2)Write following code into code behind of your ASP.NET page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rd;
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial catalog=sourav;";
        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
        connection();

       //Change the tablename here

        cmd.CommandText = "delete from loginfo where username='aaa'";
        cmd.ExecuteReader();
       

    }
}

No comments:

Post a Comment