Connectiong and Retrieving in ADO.NET
1.Connecting to Data Sources
In ADO.NET you use a [connection] object to connect to a specific data source by supllying authentication information in a connection string.The [connection] object you used depends on the type of data source.There are following object: [OleDbConnection] [SqlConnection] [Odbcconnection] [OracleConnection]
(1)Establishing a Connection 1)Closing Connections We recommend that you always close the connection after finished used it,so that the connection can be returned to the pool.The [using] block in C# auromatically disposes of the connection when the code exit the block,even in the case of an unhanldle exception.You can also use [close] or [dispose] methods of the connection object for the provider you are using. 2)Connecting to SQL Server // Assumes connectionString is a valid connection string. using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Do work here. }
参见上一个专题 Working with Connection Strings,其中访问ODBC,OLE DB,Oracle 的内容与其重复,这里不再赘述。
|