Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADO.NET connections1 Connecting to SQL Server and Oracle.

Similar presentations


Presentation on theme: "ADO.NET connections1 Connecting to SQL Server and Oracle."— Presentation transcript:

1 ADO.NET connections1 Connecting to SQL Server and Oracle

2 ADO.NET connections2 Objectives The objectives of this slide show are –to show different connection strings using very simple examples This slide show uses the connected architecture –no disconnected DataSets All examples are simple console programs –no GUI, no ASP.NET –focus on the important aspect: connections

3 ADO.NET connections3 ADO.NET in brief Connection object –created using a connection string –models a physical connection to the database Command object –created using a connection object –models a SQL statement Reader object –created using a command object –models the result of an SQL statement select or the like

4 ADO.NET connections4 Connection strings Connection string holds information on –data source (server) –database name –user name and password The structure of the connection string depends on –the database –the ADO.NET package used Further information –www.connectionstrings.com is a good sourcewww.connectionstrings.com

5 ADO.NET connections5 Trusted connections Some DBMS' can use the operating system username and password to connect to a database. Most common in Microsoft technologies –Windows + SQL Server … integrated security=SSPI; …

6 ADO.NET connections6 SQL Server connection Package System.Data.SqlClient Classes –SqlConnection –SqlCommand –SqlDataReader Example –SqlServerTest.cs

7 ADO.NET connections7 Oracle connection Package System.Data.OracleClient –in.NET framework 1.1 –must be activated during compile csc /r:System.Data.OracleClient.dll filename.cs example: compileOracle.bat Classes –OracleConnection –OracleCommand –OracleDataReader Example –OracleTest.cs

8 ADO.NET connections8 OLE DB connections OLE –Object Linking and Embedding –General Microsoft component technology OLE DB –General way of using databases in ADO.NET Not specific to any DBMS –Tries to replace ODBC –Depending on the connection string a program can connect to any database using OLE DD –Examples SqlServerOleDbTest.cs OracleOleDbTest.cs

9 ADO.NET connections9 ADO.NET interfaces The program can be made a bit more flexible (i.e. easier to change DBMS) using interfaces instead of classes Package System.Data Interfaces –IDbConnection –IDbCommand –IDataReader The connection object must be created using a real class (i.e. not an interface) Example –SqlServerTestInterface

10 ADO.NET connections10 Handling exceptions A database connection is a scarce resource –a database connection must be closed (handed back to the connection pool) after use - not matter what if we have an exception or not Code pattern SomeConnection con = null; try { con = new SomeConnection(…); use the connection } finally { if (con != null) { con.close; } }


Download ppt "ADO.NET connections1 Connecting to SQL Server and Oracle."

Similar presentations


Ads by Google