17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

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

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

Appendix B: Very Simple Connectivity Examples<br />

Within the ADO.NET example, we’ll look at a couple of different approaches to how to utilize the features<br />

provided.<br />

All code examples provided here were created with Visual Studio <strong>2008</strong>. The basic<br />

principles provided here should work just fine under other versions of Visual Studio<br />

or other editors for the languages provided, but it’s conceivable that I’ve thrown in a<br />

.NET Framework 3.5 quirk and not noticed it. (If so, apologies to those operating<br />

under different .NET Framework versions.)<br />

Some General Concepts<br />

Before we get going too deep with “just code,” there are a few key constructs that we need to understand.<br />

There are several different connectivity models that have come and gone over time; you’ll hear<br />

about such names as OLE-DB, ODBC, and, of course, ADO, among others. The connectivity model of the<br />

day these days tends to be ADO.NET or LINQ. Given the life span of these things, I wouldn’t be surprised<br />

at all if there were yet a different one by the time the next version of <strong>SQL</strong> <strong>Server</strong> comes out.<br />

Even with all the different models that have come and gone, some concepts seem to always exist in<br />

every object model — let’s take a look at these real quick:<br />

❑ Connection: The connection object is pretty much what it sounds like — the object that defines<br />

and establishes your actual communications link to your database. The kinds of parameter information<br />

that will exist for a connection object include such things as the username, password,<br />

database, and server you wish to connect to. Some of the methods that will exist include such<br />

things as connect and disconnect.<br />

❑ Command: This object is the one that carries information about what it is you want to do. Some<br />

object models will not include this object, or at least not feature it, but the concept is always there.<br />

(It is sometimes hidden as a method of the connection object.)<br />

❑ Data set: This is the result of a query — that is, if the query returns data. Some queries in which<br />

you execute, for example, a simple INSERT statement, will not return results, but, if results are<br />

returned, there will be some sort of data set (sometimes called a result set or recordset) that the<br />

query returns them into. Data set objects will generally allow for you to iterate through the<br />

records in them (often forward only in direction but usually settable to allow for more robust<br />

positioning). They will also generally allow for data to be updated, inserted, and deleted.<br />

Connectivity Examples<br />

650<br />

What we’re going to do in this section is provide some hyper (and I do mean in the extreme) examples of<br />

how to get connected in the two most common languages of the day — C# and VB.NET. For each language,<br />

we’ll show a couple of examples — one for each of two different kinds of operations (fetching a simple<br />

data set and executing a query that does not return a data set).

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

Saved successfully!

Ooh no, something went wrong!