Update database using the SqlDataAdapter

image_pdfimage_print
   


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

class Class1{
  static void Main(string[] args){
         SqlConnection thisConnection = new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
         SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT ID, FirstName FROM Employee", thisConnection);

         SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

         DataSet thisDataSet = new DataSet();

         thisAdapter.Fill(thisDataSet, "Employee");

         Console.WriteLine("name before change: {0}", thisDataSet.Tables["Employee"].Rows[9]["FirstName"]);

         thisDataSet.Tables["Employee"].Rows[1]["FirstName"] = "Inc";

         thisAdapter.Update(thisDataSet, "Employee");

         Console.WriteLine("name after change: {0}", thisDataSet.Tables["Employee"].Rows[9]["FirstName"]);


  }
}