Search This Blog

2010/01/17

Updating Existing Record using Dataset & CommandBuilder Object in C#:

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


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

da.Fill(ds, "course");
Console.WriteLine("Old Row:");
Console.Write(ds.Tables["course"].Rows[0][0].ToString());
Console.Write(" "+ ds.Tables["course"].Rows[0][1].ToString());
Console.Write(" " + ds.Tables["course"].Rows[0][2].ToString());
Console.Write(" "+ds.Tables["course"].Rows[0][3].ToString());

ds.Tables[0].Rows[0][1] = "GW BASIC";
da.Update(ds, "course");

Console.WriteLine("Edited Row:");
Console.Write(ds.Tables["course"].Rows[0][0].ToString());
Console.Write(" " + ds.Tables["course"].Rows[0][1].ToString());
Console.Write(" " + ds.Tables["course"].Rows[0][2].ToString());
Console.Write(" " + ds.Tables["course"].Rows[0][3].ToString());

Console.ReadKey();
con.Close();
}
}
}

No comments:

Post a Comment