Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

About SQLDataReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 19 Apr 2008 15:20 GMT
Hello!

I'm pretty sure that if you haven't an open a database connection when you
want to get something from the database a connection is temporarily open for
you and when you have got the data the connection is closed(returned to the
pool).

This doesn't seem to be right because I get error(no connection is open)
when I  remove this statement
"dataConnection.Open();" from the code below.

Can somebody explain why a connection is not opened ?

class Report
   {
       static void Main(string[] args)
       {
           SqlConnection dataConnection = new SqlConnection();
           try
           {
               dataConnection.ConnectionString = "Integrated
Security=true;" +
                                                 "Initial
Catalog=Northwind;" +
                                                 @"Data
Source=hempc\SQLExpress";
               //dataConnection.Open();
               Console.Write("Please enter a customer ID (% characters);
");
               string customerId = Console.ReadLine();

               SqlCommand dataCommand = new SqlCommand();
               dataCommand.Connection = dataConnection;
               dataCommand.CommandText =
                   "Select OrderID, OrderDate, " +
                   "ShippedDate, ShipName, ShipAddress, ShipCity, " +
                   "ShipCountry ";
               dataCommand.CommandText +=
                   "From Orders " +
                   "where CustomerID='" + customerId + "'";
               Console.WriteLine("About to execute {0}\n\n",
dataCommand.CommandText);

               SqlDataReader dataReader =
dataCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

               while (dataReader.Read())
               {
                   int orderId = dataReader.GetInt32(0);

                   if (dataReader.IsDBNull(2))
                   {
                       Console.WriteLine("Order {0} not yet shipped\n\n",
orderId);
                   }
                   else
                   {
                       DateTime orderDate = dataReader.GetDateTime(1);
                       DateTime shipDate = dataReader.GetDateTime(2);
                       string shipName = dataReader.GetString(3);
                       string shipAddress = dataReader.GetString(4);
                       string shipCity = dataReader.GetString(5);
                       string shipCountry = dataReader.GetString(6);
                       Console.WriteLine(
                           "Order {0}\nPlaced {1}\nShipped {2}\n" +
                           "ToAdress {3}\n{4}\n{5}\n{6}\n\n", orderId,
orderDate,
                           shipDate, shipName, shipAddress, shipCity,
shipCountry);
                   }
               }

               dataReader.Close();
           }
           catch (Exception e)
           {
               Console.WriteLine("Error accessing the database: " +
e.Message);
           }

           finally
           {
               System.Data.ConnectionState state = dataConnection.State;
               dataConnection.Close();
           }
       }
   }
}
Jon Skeet [C# MVP] - 19 Apr 2008 16:50 GMT
On Apr 19, 7:20 am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> I'm pretty sure that if you haven't an open a database connection when you
> want to get something from the database a connection is temporarily open for
> you and when you have got the data the connection is closed(returned to the
> pool).

I believe that's the case for SqlDataAdapter, but I can't see any
documentation saying it's the case for calling
SqlCommand.ExecuteReader directly.

Jon
Arne Vajhøj - 19 Apr 2008 17:05 GMT
> On Apr 19, 7:20 am, "Tony Johansson" <johansson.anders...@telia.com>
> wrote:
[quoted text clipped - 6 lines]
> documentation saying it's the case for calling
> SqlCommand.ExecuteReader directly.

The reader has to support data sizes that can not be in memory,
so ExecuteReader can not release the connection.

.ExecuteReader(CommandBehavior.CloseConnection) must be the
closest.

Arne
Jon Skeet [C# MVP] - 19 Apr 2008 17:45 GMT
> > I believe that's the case for SqlDataAdapter, but I can't see any
> > documentation saying it's the case for calling
> > SqlCommand.ExecuteReader directly.
>
> The reader has to support data sizes that can not be in memory,
> so ExecuteReader can not release the connection.

When the reader is closed, I think it's reasonable for it to close the
connection when CommandBehavior.CloseConnection has been specified -
I'd certainly expect to have to retrieve all the data I wanted (even
from blobs/clobs) before that point.

However, I believe the OP is more concerned that it's not *opening*
the connection.

Jon
Arne Vajhøj - 19 Apr 2008 17:56 GMT
>>> I believe that's the case for SqlDataAdapter, but I can't see any
>>> documentation saying it's the case for calling
[quoted text clipped - 9 lines]
> However, I believe the OP is more concerned that it's not *opening*
> the connection.

Ah. Then everything makes sense.

Arne
Peter Bromberg [C# MVP] - 19 Apr 2008 20:41 GMT
Only the DataAdapter class family supports managing the opening / closing of
the Connection object.  SqlCommand with ExecuteReader, ExecuteScalar, etc. -
you have to take care of it yourself. As mentioned,
CommandBehavior.CloseConnection on a SqlDataReader command is the closest you
can come to this -- when you close the Reader the connection is also closed
and returned to the Connection Pool.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net

> Hello!
>
[quoted text clipped - 84 lines]
>     }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.