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 7954601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:28:41+00:00 2026-06-04T03:28:41+00:00

When I connect to a SQL datasource in my C# application, I can fill

  • 0

When I connect to a SQL datasource in my C# application, I can fill a dataset using the following code. Note that I am not explictly opening a connection to the datasource.

SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password");
SqlCommand cmd = new SqlCommand("SELECT * FROM Example WHERE value = value");
cmd.Connection = cw_con;

//Create DataSet
DataSet cw_ds = new DataSet("cw_ds");
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
//Execute Command and Fill DataSet
da.Fill(cw_ds);
cw_ds.Clear();

However, If I want to execute a nonquery such as a INSERT INTO or UPDATE why do I have to explicitly open the connection by using connection.Open();?

SqlConnection cw_con = new SqlConnection("Server=Server;Database=Database;User=User;password=password");
cw_con.Open();
SqlCommand cmd = new SqlCommand("UPDATE example SET value = value WHERE value = value");
cmd.Connection = cw_con;
cmd.ExecuteNonQuery();
cw_con.Close();
  • 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-06-04T03:28:43+00:00Added an answer on June 4, 2026 at 3:28 am

    That is because a DataAdapter opens the connection implicitely when it’s closed initially. It’ll close it afterwards if it was closed before, otherwise the connection stays open.

    From MSDN:

    The Fill method retrieves the data from the data source using a SELECT
    statement. The IDbConnection object associated with the select command
    must be valid, but it does not need to be open. 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.

    When using a SqlCommand you must explicitely open and close connections yourself.

    As a side note:

    1. Use parameters to avoid SQL-Injection
    2. Use using-statement for anything implementing IDisposable, especially Connections since it ensures that it gets disposed/closed as soon as possible, even in the case of an error
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always thought that in order to connect to SQL server using windows
I'm developing an ASP.NET app (C#) that connect to SQL Server 2008 using ADO.NET
I am writing a windows mobile application that uses SQL CE. It is not
When our application starts up it can't seem to connect to the database (using
I'm trying to connect to SQL Server on Ubuntu 9.04 using Ruby. I translated
When I attempt to connect to SQL server using enterprise manager I get the
I am using c# to connect to sql server on another computer on the
I have a standalone application that needs to connect to a Sybase database via
I'm using MS Visual Studio 2010 to create an application with SQL Server 2008
I have a web project in eclipse using glassfish. I have the following datasource

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.