Search This Blog

Sunday, January 17, 2010

Deleting Rows from Table using CommandBuilder,dataAdapter,Dataset in C#:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace Deleting_Rows
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Initial Catalog=sangram;Data Source=(local)");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from course", con);
SqlCommandBuilder sbuilder = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "course");

//defining primary key
DataColumn[] keys= new DataColumn[1];
keys[0] = ds.Tables[0].Columns["cno"];
ds.Tables[0].PrimaryKey = keys;

DataRow findrow = ds.Tables[0].Rows.Find("26");

if (findrow != null)
{
findrow.Delete();
da.Update(ds, "course");
}
else
{
Console.WriteLine("No Such Record Found");
}
con.Close();
Console.ReadKey();
}
}
}

No comments:

Post a Comment