1

I am trying to learn about how to work with databases in C# and I have gotten to a part in a tutorial when I have to work with DataSet , SqlDataAdapter and SqlCommandBuilder.This is the code I wrote in the example in the tutorial:

 public void InitData() {
        //instantiate the connection
        conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\Projects IDE\\Visual Studio\\Exercitii\\Console.app\\WindowsFormsApplication1\\WindowsFormsApplication1\\PlanetWrox.mdf\";Integrated Security=True;User Instance=True");

        //1.instantiate a new DataSet
        dsCustomers = new DataSet();

        //2.init SqlDataAdapter with select command and connection
        daCustomers = new SqlDataAdapter("SELECT Id ,Name, SortOrder FROM Genre", conn);

        // 3. fill in insert, update, and delete commands
        SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);

        // 4. fill the dataset
        daCustomers.Fill(dsCustomers, tableName);
    }

    public void btnUpdateClicked(object sender, EventArgs e) {
        // write changes back to DataBase
        daCustomers.Update(dsCustomers, tableName);

    }   

There are a couple of things I do not understand here:

The first thing I noticed is that I do not have to open and close the database.From what limited knoledge I have about databases I know that in order to acces the data you have to open a connection to it and after you are done you have to close it.This must happen somewhere behind the scene.Is that righT? If that is so witch method does that?

The second question is regarding the SqlDataAdapter and SqlCommandBuilder.I do not understand what does SqlCommandBuilder does here.Isen't the SqlDataAdapter the one that is executing the sql query?

1
  • 1
    When you call daCustomers.Fill(..) ADO.NET open and close connection to database for you Commented Oct 9, 2012 at 11:28

6 Answers 6

2

The first thing I noticed is that I do not have to open and close the database.

SqldataAdapter.Fill will open/close the connection for you.

If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.

The SqlCommandBuilder generates INSERT, UPDATE, or DELETE statements from the metadata of your provided select-statement. On this way you can call daCustomers.Update and all changes made to the DataSet will automatically be updated in the database.

Sign up to request clarification or add additional context in comments.

Comments

1

dsCustomers.Fill opens and closes connection for you. SqlCommandBuilder creates insert, update, and delete based on your select statement.

Comments

1

ADO.NET handles that task for you, when you Fill your table with data

Comments

0
//instantiate the connection
  using (SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\Projects IDE\\Visual Studio\\Exercitii\\Console.app\\WindowsFormsApplication1\\WindowsFormsApplication1\\PlanetWrox.mdf\";Integrated Security=True;User Instance=True"))
  {
    //1.instantiate a new DataSet
    dsCustomers = new DataSet();

    //2.init SqlDataAdapter with select command and connection
    daCustomers = new SqlDataAdapter("SELECT Id ,Name, SortOrder FROM Genre", conn);

    // 3. fill in insert, update, and delete commands
    SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);

    // 4. fill the dataset
    daCustomers.Fill(dsCustomers, tableName);
  }

This would automatically close the connection and Dispose, without you having to bother about it.

Comments

0

Fill will make the all the resources locked that are involved in the process. Also as per the sql settings it may block other resources too.

If you are using this code for some real time work..Please choose it only if you need this.

Comments

0

For your first thing:

We use conn.open(); to open the connection and conn.close(); to close the connection.

Here in your program you are not connecting to database like to sqlserver. You have provided shown physical path to the file.

See the below for second thing:

SqlCommandBuilder Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.