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.

Using Database Connections ❘ 821<br />

In case you forget the format of database connection strings (as many of us do now<br />

<strong>and</strong> then), the following URL is very h<strong>and</strong>y: www.connectionstrings.com.<br />

The example opens a database connection using the defi ned connection string <strong>and</strong> then closes that<br />

connection. Once the connection has been opened, you can issue comm<strong>and</strong>s against the data source, <strong>and</strong><br />

when you are fi nished, the connection can be closed.<br />

SQL Server has another mode of authentication — it can use Windows - integrated security, so that the<br />

credentials supplied at logon are passed to SQL Server. This is accomplished by removing the uid <strong>and</strong> pwd<br />

portions of the connection string, <strong>and</strong> adding in Integrated Security=SSPI .<br />

In the download code available for this chapter, you will fi nd the fi le login.cs , which simplifi es the<br />

examples in this chapter. It is linked to all the sample code <strong>and</strong> includes database connection information<br />

used for the examples; you can alter this to supply your own server name, user, <strong>and</strong> password as<br />

appropriate. This, by default, uses Windows - integrated security; however, you can change the username <strong>and</strong><br />

password as appropriate.<br />

managing Connection strings<br />

In the initial release of .<strong>NET</strong>, it was up to the developer to manage the database connection strings, which<br />

was often done by storing a connection string in the application confi guration fi le or, more commonly, hard -<br />

coded somewhere within the application itself.<br />

Beginning with .<strong>NET</strong> 2.0, you have a predefi ned way to store connection strings, <strong>and</strong> even use database<br />

connections in a type - agnostic manner — for example, it is now possible to write an application <strong>and</strong> then<br />

plug in various database providers, all without altering the main application.<br />

To defi ne a database connection string, you should use the section of the<br />

confi guration fi le. Here, you can specify a name for the connection <strong>and</strong> the actual database connection<br />

string parameters; in addition, you can also specify the provider for this connection type. Here is an<br />

example:<br />

< configuration ><br />

...<br />

< connectionStrings ><br />

< add name="Northwind"<br />

providerName="System.Data.SqlClient"<br />

connectionString="server=(local);integrated security=SSPI;database=Northwind" / ><br />

< /connectionStrings ><br />

< /configuration ><br />

You use this same connection string in the other examples in this chapter.<br />

code download Connection String Example.txt<br />

Once the database connection information has been defi ned within the confi guration fi le, you then need<br />

to utilize this within the application. You will most likely want to create a method such as the following to<br />

retrieve a database connection based on the name of the connection:<br />

private DbConnection GetDatabaseConnection ( string name )<br />

{<br />

ConnectionStringSettings settings =<br />

ConfigurationManager.ConnectionStrings[name];<br />

DbProviderFactory factory = DbProviderFactories.GetFactory<br />

( settings.ProviderName );<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!