Sunday, January 17, 2010

Inserting Record into Existing DataBase using commandbuilber,datarow,Adapter:




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

namespace Adding_Rows_to_table
{
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");
Console.WriteLine("Number of Rows Before Adding New Record :" + ds.Tables[0].Rows.Count.ToString());

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

drow["cno"] = 27;
drow["cname"] = "CORE JAVA";
drow["duration"] = 2;
drow["fees"] = 4000 ;

ds.Tables[0].Rows.Add(drow);
da.Update(ds, "course");

Console.WriteLine(ds.Tables[0].Rows.Count.ToString());

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

}
}
}

No comments:

Post a Comment