Summary

In this chapter, you learned how to connect to a database. There are three Connection classes: SqlConnection, OleDbConnection, and OdbcConnection. You use an object of the SqlConnection class to connect to a SQL Server database, an object of the OleDbConnection class to connect to any database that supports OLE DB, such as Oracle or Access, and an object of the OdbcConnection class to connect to any database that supports ODBC. Ultimately, all communication with a database is done through a Connection object.

ADO.NET automatically stores database connections in a pool. When you close a connection, that connection isn't actually closed. Instead, your connection is marked as unused and stored in the pool ready to be used again. If you then supply the same details in the connection string (same database, username, password, and so on), then the connection from the pool is retrieved and returned to you. You then use that same connection to access the database. Connection pooling offers a great performance improvement, because you don't have to wait for a new connection to the database to be established when there's a suitable connection already available in the pool.

You use a Connection object's State property to get the current state of the connection to the database. The State property returns a constant from the ConnectionState enumeration.

You use a Connection object's StateChange event to monitor changes in the state of your Connection object. You use a Connection object's InfoMessage event to monitor warning or information messages produced by the database. You can produce such messages using the SQL Server PRINT or RAISERROR statements.

To create a SqlConnection object using Visual Studio .NET, you drag a SqlConnection object from the Data tab of the Toolbox to your form. You can also drag an OleDbConnection object from the Toolbox to your form.

In the next chapter, you'll learn how to execute database commands.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net