asp.net.ph

SqlConnection.Open Method

System.Data.SqlClient Namespace   SqlConnection Class


Opens a database connection with the property settings specified by the ConnectionString.

[ VB ]
NotOverridable Public Sub Open ( )

[ C# ]
public void Open ( );

[ C++ ]
public: __sealed void Open ( );

[ C++ ]
public function Open ( );

Exceptions


Exception Type Condition
InvalidOperationException Occurs when the connection is already open.
SqlException Occurs when a connection-level error occurred while opening the connection.

Remarks

The SqlConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to the data source.

NOTE: If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose.

Example

The following example initializes and opens an SqlConnection, displays some of its properties, then closes the connection.

protected void Page_Load ( Object Src, EventArgs e ) {
   // specify the data source
   SqlConnection myConn = new SqlConnection (
      "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + 
      Server.MapPath ( "~/app_data/pubs.mdb" ) );
   // open the data connection
   myConn.Open ( );

   // retrieve properties of the connection object
   ...

   // close the data connection
   myConn.Close ( );
}

 Show me 

See Also

SqlConnection Members   Close   StateChange Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph