Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whats the use of data adapter

Tags:

c#

ado.net

Can anyone explain why an SqlDataAdapter is used in the following code? The code is working fine without this adapter.

Also, why do we use DataAdapter? Please help me to understand this DataAdapter usage.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=.....\\SQLEXPRESS;Initial Catalog=......;Integrated Security=True");
                con.Open();

                SqlDataAdapter da =new SqlDataAdapter(); // Why use `SqlDataAdapter` here?
                SqlCommand sc = new SqlCommand("insert into bhargavc values(" + textBox1.Text + "," + textBox2.Text + ");", con);
                var o = sc.ExecuteNonQuery();
                MessageBox.Show(o + "record to be inserted");
                con.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("error in the code");
            }
        }      

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}
like image 865
Duddupudi Bhargav Avatar asked Dec 17 '25 19:12

Duddupudi Bhargav


1 Answers

Data adapter works like a mediator between the database and the dataset. However, a data adapter cant store data. It just gives data from the database to the dataset.

For Example:

A water pipe is used to bring water from a source (well, pond, etc.) to a destination. However, the pipe isn't used to store water. In the same way, a data adapter (like a water pipe) sends data from the database to a dataset.

This should give a clearer understanding about data adapters.

like image 184
Ajay Avatar answered Dec 20 '25 07:12

Ajay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!