Tuesday, April 8, 2014

Disconnected or Offline mode Database Access With ADO.NET

1. To Insert Data into Into Database

 
 
 SqlConnection con = new SqlConnection(@"Data Source=GAUTAM-PC;Initial Catalog=Emp;Integrated Security=True");
conn.Open();

            SqlDataAdapter sda = new SqlDataAdapter("Select * from emp", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "Emp");

            DataTable dt = ds.Tables["Emp"];

            DataRow dr = dt.NewRow();
            dr["Empno"] = Convert.ToInt32(textBox1.Text);
            dr["Ename"] = textBox2.Text;
            dr["Salary"] = Convert.ToInt32(textBox3.Text);

            dt.Rows.Add(vDr);
            
            if (ds.HasChanges())
            {
                SqlCommandBuilder cmdbldr = new SqlCommandBuilder(sda);
                vAdap.Update(ds, "Emp");
                MessageBox.Show("Record successfully saved");
               
            }
            conn.Close(); 
 

2. To Update


            SqlConnection con = new SqlConnection(@"Data Source=GAUTAM-PC;Initial Catalog=Emp;Integrated Security=True");
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("Select * from emp", conn);
            DataSet ds = new DataSet();
            vAdap.FillSchema(ds, SchemaType.Source);
            vAdap.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            vAdap.Fill(ds, "EmpNew");
            DataTable dt = ds.Tables["EmpNew"];
           
            DataRowCollection drc = dt.Rows;
            DataRow rr=drc.Find("101");
            if (dr == null)
            {
                MessageBox.Show("Sorry no record found");
            }
            else
            {
                dr.BeginEdit();
               
                dr["Ename"] = "Microsoft";
                dr["Salary"] = 120000;
                dr.EndEdit();
               
                if (vDs.HasChanges())
                {
                   
                    SqlCommandBuilder cmdbldr = new SqlCommandBuilder(sda);
                    vAdap.Update(ds, "EmpNew");
                    MessageBox.Show("Record successfully updated");
                }
                conn.Close();
            }
        }

3. To Delete


SqlConnection con = new SqlConnection(@"Data Source=GAUTAM-PC;Initial Catalog=Emp;Integrated Security=True");
            SqlDataAdapter sda = new SqlDataAdapter("Select * from emp, conn);
            DataSet ds = new DataSet();
            vAdap.FillSchema(ds, SchemaType.Source);
            vAdap.MissingSchemaAction = MissingSchemaAction.AddWithKey;

 
            vAdap.Fill(ds, "EmpNew");
            DataTable dt = ds.Tables["EmpNew"];

 

 
            DataRowCollection drc = dt.Rows;
            DataRow dr = drc.Find("101");
            if (dr == null)
            {
                MessageBox.Show("Sorry no record found");
            }
            else
            {
                //New values for updation
                dr.Delete();
                //Now we need to sync the data back to the database
                if (ds.HasChanges())
                {
                   
                    SqlCommandBuilder cmdbldr = new SqlCommandBuilder(sda);
                    vAdap.Update(ds, "EmpNew");
                    MessageBox.Show("Record successfully updated");

 
                }
                conn.Close();

No comments:

Post a Comment