Duddupudi Bhargav
Duddupudi Bhargav

Reputation: 47

whats the use of data adapter

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)
        {
        }
    }
}

Upvotes: 2

Views: 7999

Answers (4)

Mathusan
Mathusan

Reputation: 1

  1. To fill either dataset or datatable
  2. You don't have to close your SQL connection explicitly like .Close()

Upvotes: 0

Brian
Brian

Reputation: 5119

There are several reasons to use a DataAdapter:

  1. You can't fill a DataSet without one.
  2. When the adapter completes its .Fill() method, it will close the connection for you; you don't have to explicitly call the .Close() method on your connection object. Although, it is still good practice to.

In your case, it isn't necessary to have one though. But, if you did want to use one, the implementation would look like this:

SqlDataAdapter da = new SqlDataAdapter();     
DataSet ds = new DataSet();
da.Fill(ds);

From there, there are further actions that you can take on the ds object like exporting to Excel via Interop, filling a DataGridView or even updating a database table.

Upvotes: 4

Ajay
Ajay

Reputation: 2080

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.

Upvotes: 6

Guffa
Guffa

Reputation: 700322

A data adapter is used to read data from a data reader into a DataTable or a DataSet.

As you are not reading any data from the database in this code, the data adapter is completely superflous.


As a side note, you should use parameters instead of putting values directly into the query. Your code is totally open to SQL injection attacks.

Upvotes: 5

Related Questions