Use of CommandBuilder


Unknown | 03:55 |

Use of CommandBuilder

CommandBuilder builds “Parameter” objects automatically and SqlCommandBuilder object to automatically create  the DeleteCommand, the InsertCommand, and the UpdateCommand properties of the SqlCommand object for a SqlDataAdapter object,
Insert Data using SqlCommandBuilder

using System;
using System.Data;
using System.Data.SqlClient;

class stringbuildinginsert
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("server=(local)\\SQLEXPRESS;database=Emp;Integrated
Security=SSPI;"
);
SqlDataAdapter sda = new SqlDataAdapter("SELECT ID, FirstName, LastName FROM Employee", con);
SqlCommandBuilder Cmdb = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();

sda.Fill(ds);

DataRow drow = ds.Tables[0].NewRow();

drow["ID"] = 200;
drow["FirstName"] = "Kamal";
drow["LastName"] = "Mishra";
ds.Tables[0].Rows.Add(MyRow);
sda.Update(ds);
}
}


Delete Data using SqlCommandBuilder

using System;
using System.Data;
using System.Data.SqlClient;

class stringbuildinginsert
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("server=(local)\\SQLEXPRESS;database=Emp;Integrated
Security=SSPI;"
);
SqlDataAdapter sda = new SqlDataAdapter("SELECT ID, FirstName, LastName FROM Employee", con);
SqlCommandBuilder Cmdb = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();

sda.Fill(ds);

DataColumn[] dc = new DataColumn[1];

dc[0] = ds.Tables[0].Columns[0];
dst.Tables[0].PrimaryKey = dc;

DataRow drow = MyDataSet.Tables[0].Rows.Find(1);

drow.Delete();
sdar.Update(ds);
}
}

0 comments:

Post a Comment