Insert, Update and Delete_Query
INSERT-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Registration : System.Web.UI.Page
{ SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{ //step 1- connection to database
string path =@"Data Source=49.50.88.43;Initial Catalog=grkistdb;Persist Security Info=True;User ID=grkistuser;Password=q2oR5w_356";
cn = new SqlConnection(path);
cn.Open();
//step 2- Command (write a query)
string q = " insert into Chitransh_stu values ('"+TextBox1.Text+"', '"+TextBox2.Text+"', '"+TextBox3.Text+"', )"
cm = new SqlCommand(q, cn);
//step 3- execute the query
cm.ExecuteNonQuery();
}
}
UPDATE-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Registration : System.Web.UI.Page
{ SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{ //step 1- connection to database
string path =@"Data Source=49.50.88.43;Initial Catalog=grkistdb;Persist Security Info=True;User ID=grkistuser;Password=q2oR5w_356";
cn = new SqlConnection(path);
cn.Open();
//step 2- Command (write a query)
string q = " update from Chitransh_stu set column_name='"+control_name.Text+"' where id='"+control_name.Text+"'"
cm = new SqlCommand(q, cn);
//step 3- execute the query
cm.ExecuteNonQuery();
}
}
How update query executes-
" update from Table_name set
column_name='"+control_name.Text+"' where
id='"+control_name.Text+"'"
Column_name- here we give the columns those we want to update.
Control_name- the control data is supplied to data base like textbox.
Where condition- here we give the reference to update the particular row / record like roll no, id etc.
It is execute non query.
DELETE-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Registration : System.Web.UI.Page
{ SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{ //step 1- connection to database
string path =@"Data Source=49.50.88.43;Initial Catalog=grkistdb;Persist Security Info=True;User ID=grkistuser;Password=q2oR5w_356";
cn = new SqlConnection(path);
cn.Open();
//step 2- Command (write a query)
string q = " delete from Chitransh_stu "' where id='"+control_name.Text+"'"
cm = new SqlCommand(q, cn);
//step 3- execute the query
cm.ExecuteNonQuery();
}
}
How it works-
delete query is used to delete the entire row with reference to any column using where condition or the entire table.
It is also execute non query.
Comments
Post a Comment