Sunday, January 8, 2012

command builder in asp.net

Title:How to use Command builder in asp.net using c#.net
The command builder class is used to generate the SQL statements,command builder takes constructed parameter is data adapter depending on the data adapter columns ,generates SQL statement
Methods:

GetInsertCommand method:This method generates the insert statement
GetDeleteCommand method:This method generates the delete statement
GetUpdateCommand method:This method generates the update statement
here i will show an example to insert the rows using data table class.For this i have taken one grid view,two text box's and three buttons
Code behind:
Using System.Data.Oledb;

OleDbConnection cn=new OleDbConnection("provider=MSADAORA.1;serid=scott;password=tiger")
OleDbDataAdapter ad;
DataSet ds=new dataSet();
DataTable dt=new DataTable();
DataRow dr;
//button _click(bind the data to grid view)
{
ad1=new OleDbDataAdapter("select*from customers",con)
ad1.fill(ds,"c");
dt=ds.Tables["c"];
Gvcustomers.DataSource=dt;
}
//button2_click(add to Table rows using command builder)
{
OleDbCommandBuilder cbd=new OleDbCommandBuilder(ad1);
dr["cno"]=convert.Toint16(txt1.Text);
dr["cname"]=(txt2.Text);
dt.Rows.Add(dr);
ad1.InsertCommand=cbd.GetInsertCommand();

}
//button3_click(update using command builder)
{
ad1.Update(ds,"s");
}

No comments:

Bel