using SqlDataAdapter Update table — ADO.Net , C Sharp — ASP.Net Example
ASP.Net Example-using SqlDataAdapter Update table

Saturday, May 21, 2011

using SqlDataAdapter Update table

using System;

using System.Data;

using System.Data.SqlClient;



class UpdateusingdataobjAdapter
{
static void Main()
{
string cn="server=localhost;database=DBTEST;uid=sa;pwd=sa";
SqlConnection Connection = new SqlConnection(cn);
string query = @"select * from tblemp ";
string update = @"update tblemp set name = @name where id = @id";
try
{

   SqlDataobjAdapter objAd = new SqlDataobjAdapter();

   objAd.SelectCommand = new SqlCommand(query, Connection );
   DataSet objDs = new DataSet();  

   objAd.Fill(objDs, "tblemp");
   DataTable objDt = objDs.Tables["tblemp"];

   objDt.Rows[0]["name"] = "Ravi";

   foreach (DataRow row in objDt.Rows)
   {

    Console.WriteLine(

                  "{0} {1}",

                  row["name"].ToString(),

                  row["lastname"].ToString();

            }



     // Update tblemps

    SqlCommand objcmd = new SqlCommand(update, Connection );

    objcmd.Parameters.Add("@name",SqlDbType.NVarChar,15, "name");

    SqlParameter parm = objcmd.Parameters.objAdd("@id",SqlDbType.Int,4,"id");

    parm.SourceVersion = DataRowVersion.Original;

    objAd.UpdateCommand = objcmd;

    objAd.Update(objDs, "tblemp");

    }
catch(Exception e)
 {

            Console.WriteLine("Error: " + e);

 }
finally
 {

            Connection.Close();

 }

      } 

   }

No comments:

Post a Comment

ASPdotNET-Example