15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

820 ❘ ChaPTer 30 cOre AdO.net<br />

The classes <strong>and</strong> interfaces used for data access in the .<strong>NET</strong> Framework are introduced<br />

in the course of this chapter. The focus is mainly on the SQL classes used when<br />

connecting to the database because the Framework SDK samples install a SQL Server<br />

Express database (SQL Server). In most cases, the OLE DB <strong>and</strong> ODBC classes mimic<br />

the SQL code exactly.<br />

using daTabase ConneCTions<br />

To access the database, you need to provide connection parameters, such as the machine that the database<br />

is running on <strong>and</strong> possibly your login credentials. Anyone who has worked with ADO will be familiar<br />

with the .<strong>NET</strong> connection classes: OleDbConnection <strong>and</strong> SqlConnection . Figure 30 - 1 shows two of the<br />

connection classes <strong>and</strong> includes the class hierarchy.<br />

IDBConnection<br />

DBConnection<br />

DBConnectionBase<br />

OleDbConnection SqlConnection Other ...<br />

figure 30-1<br />

The examples in this chapter use the Northwind database, which you can fi nd online. The following code<br />

snippet illustrates how to create, open, <strong>and</strong> close a connection to the Northwind database:<br />

using System.Data.SqlClient;<br />

string source = "server=(local);" +<br />

"integrated security=SSPI;" +<br />

"database=Northwind";<br />

SqlConnection conn = new SqlConnection(source);<br />

conn.Open();<br />

// Do something useful<br />

conn.Close();<br />

The connection string should be very familiar to you if you have used ADO or OLE DB before — indeed,<br />

you should be able to cut <strong>and</strong> paste from your old code if you use the OleDb provider. In the example<br />

connection string, the parameters used are as follows (the parameters are delimited by a semicolon in<br />

the connection string):<br />

➤<br />

➤<br />

➤<br />

server=(local) — This denotes the database server to connect to. SQL Server permits a number of<br />

separate database server instances to be running on the same machine, <strong>and</strong> here you are connecting<br />

to the default SQL Server instance. If you are using SQL Express, change the server part to<br />

server=./sqlexpress .<br />

integrated security=SSPI — This uses Windows Authentication to connect to the database,<br />

which is highly recommended over using a username <strong>and</strong> password within the source code.<br />

database=Northwind — This describes the database instance to connect to; each SQL Server process<br />

can expose several database instances.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!