Showing posts with label how to work SQLcommandbuilder. Show all posts
Showing posts with label how to work SQLcommandbuilder. Show all posts

Friday, May 13, 2016

CommandBuilder in ADO.Net

The CommandBuilder use to generate update, Delete, insert commands on a single database table for data adapter, in other words it is used to permanently save the record in the database.

The Example of the CommandBuilder is given below: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication20
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void button1_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = textBox1.Text;
            dr[1] = textBox2.Text;
            dr[2] = textBox3.Text;
            ds.Tables[0].Rows.Add(dr);
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            da.Update(ds.Tables[0]);
            MessageBox.Show("save");
        }
    }
}
Output