• 0

[ASP.NET] I have a function that returns a dataset


Question

Hello,

So I have a function that returns a DataSet. When do I close the databse connection? Can I close it before returning the DataSet?

Example:

	public static DataSet Addresses_for_a_Client_by_ID(string cID)
	{
		/* snipped */

		SqlDataAdapter da = new SqlDataAdapter();
		da.SelectCommand = cmd;
		DataSet ds = new DataSet();
		da.Fill(ds, "ClientAddresses");

		// Can I do: connection.Close(); here? 
		return ds;
	}

4 answers to this question

Recommended Posts

  • 0

Yep. You can close the connection before returning the dataset. Ideally you would want to do "da.Dispose()" between "da.Fill(...)" and "return ds;" The dataset holds the results of the query in its own data structure, so once you've got the data, you don't need to be connected to the database anymore.

  • 0
  Majesticmerc said:
Yep. You can close the connection before returning the dataset. Ideally you would want to do "da.Dispose()" between "da.Fill(...)" and "return ds;" The dataset holds the results of the query in its own data structure, so once you've got the data, you don't need to be connected to the database anymore.

DataSet.Dispose doesn't really do anything, its an inherited member from a lower level of the abstraction tree.

  • 0
  Antaris said:
DataSet.Dispose doesn't really do anything, its an inherited member from a lower level of the abstraction tree.

Ah fair enough. Cheers :)

Edited by Majesticmerc
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Everything's OK! 🤣🤣🤣  
    • XnView 2.52.2 by Razvan Serea XnView is freeware software that allows you to view and convert graphic files. It's very fast and easy to use. But more importantly it supports more than 500 graphics formats! Import about 500 graphic file formats Export about 50 graphic file formats Multipage TIFF, Animated GIF, Animated ICO support Image IPTC, EXIF metadata support EXIF auto rotation support IPTC editing Resize, rotate, crop support Lossless rotate & crop (jpeg) support Adjust brightness, contrast... Auto levels, contrast Modify number of colors Apply filters (blur, average, emboss, ...) Apply effects (lens, wave, ...) Fullscreen mode Slide show with effects Batch convert, batch rename Create WEB page easily Screen capture Create contact Sheet Create or edit Multi-page file (TIFF, DCX, LDF) TWAIN & WIA support (Windows only) Print support (Windows only) Drag & Drop support (Windows only) Compare image side by side Filmstrip layout 44 languages support (Windows only) XnView 2.52.2 changelog: NConvert: -xyb for JPEGli XnView vs XnView MP: XnView is the classic, lightweight version with basic features and Windows-only support. XnView MP is the modern, cross-platform version with an improved interface, better performance, and extended format support. Download: XnView 2.52.2 Installer | Standalone | ~24.0 MB (Non-commercial freeware) Download: XnView MP 1.9.2 64-bit | Standalone | ~100.0 MB Download: XnView MP 1.9.2 32-bit | Standalone | ~60.0 MB View: XnView Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Correct. You don’t buy anything that’s free (if it were).
    • Vivaldi 7.4.3684.52 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Vivaldi 7.4.3684.52 changelog: [Address bar] URL is searched instead of opening (VB-117198) [Chromium] Update to 136.0.7103.179 [Settings][Workspaces] Add Workspace Rule button hidden by 4th rule (VB-117181) Download: Vivaldi 64-bit | 125.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
    • Week One Done
      Karan Khanna earned a badge
      Week One Done
    • First Post
      MikeK13 earned a badge
      First Post
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      687
    2. 2
      ATLien_0
      268
    3. 3
      Michael Scrip
      202
    4. 4
      +FloatingFatMan
      169
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!