Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:00:04+00:00 2026-05-25T16:00:04+00:00

Does SqlDataAdapter close the SqlConnection after the Fill() function or do I need close

  • 0

Does SqlDataAdapter close the SqlConnection after the Fill() function or do I need close it myself?

string cnStr = @"Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
cn = new SqlConnection(cnStr);
SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
adapter.Fill(ds);

cn.Close() // ????????

Console.WriteLine(ds.Tables[0].Rows.Count);
Console.WriteLine(cn.State);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:00:05+00:00Added an answer on May 25, 2026 at 4:00 pm

    In your current usage, it will close 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.

    http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx

    I think it’s always better to explicitly cater for it yourself with a using statement:

    using (SqlConnection conn = new SqlConnection(""))
    {
        conn.Open();
    
        // Do Stuff.
    
    } // Closes here on dispose.
    

    This is often more readable and doesn’t rely on people understanding the inner workings of SqlDataAdapter.Fill, just the using statement and connections.

    However, if you know the connection is closed before the adapter uses it (as in, you’ve just created the connection) and it’s not used for anything else, your code is perfectly safe and valid.

    Personally, I’d write something like this:

        string cnStr = "Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
        DataSet ds = new DataSet();
    
        using (SqlConnection cn = new SqlConnection(cnStr))
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn))
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        { 
            conn.Open();
            adapter.Fill(ds);       
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code mycon=new SqlConnection(); mycon.ConnectionString='Provider =Microsoft.ACE.OLEDB.12.0';Data Source='G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb'; myds=new DataSet(); mytable =
Does anyone know if the SqlDataAdapter.Dispose method actually closes or disposes any SqlConnections? I
Edit: I am using SqlDataAdapters to fill the data sets. Sorry--I should have been
Does anyone know how to use the define function and pass a variable into
Does Google force employees who have offers from Facebook to leave immediately?
Does anyone remember the XMP tag? What was it used for and why was
Does the Java language have delegate features, similar to how C# has support for
Does anybody know any good resources for learning how to program CIL with in-depth
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.