Opening and Closing Connections

To open a connection to the data store, you simply invoke the SqlConnection.Open() method. Likewise, to close the connection, invoke the SqlConnection.Close() method.

Note

If connection pooling is supported, opening a connection will try to use a connection from the pool; otherwise, a new connection is established to the data store. Also, closing a pooled connection adds the connection to the pool instead of actually closing it. Connection pooling is discussed later in this chapter.

Listing 18.1 illustrates the opening and closing of a SQL Server connection.

Listing 18.1. Opening and Closing a Connection
1: program test; 2: {$APPTYPE CONSOLE} 3: uses 4: System.Data.SQLClient; 5: const 6: CNSTR = 'server=localhost;database=Northwind;Trusted_Connection=Yes'; ...

Get Delphi for .NET Developer’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.