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

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

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Appendix B: Very Simple Connectivity Examples Within the ADO.NET example, we’ll look at a couple of different approaches to how to utilize the features provided. All code examples provided here were created with Visual Studio 2008. The basic principles provided here should work just fine under other versions of Visual Studio or other editors for the languages provided, but it’s conceivable that I’ve thrown in a .NET Framework 3.5 quirk and not noticed it. (If so, apologies to those operating under different .NET Framework versions.) Some General Concepts Before we get going too deep with “just code,” there are a few key constructs that we need to understand. There are several different connectivity models that have come and gone over time; you’ll hear about such names as OLE-DB, ODBC, and, of course, ADO, among others. The connectivity model of the day these days tends to be ADO.NET or LINQ. Given the life span of these things, I wouldn’t be surprised at all if there were yet a different one by the time the next version of SQL Server comes out. Even with all the different models that have come and gone, some concepts seem to always exist in every object model — let’s take a look at these real quick: ❑ Connection: The connection object is pretty much what it sounds like — the object that defines and establishes your actual communications link to your database. The kinds of parameter information that will exist for a connection object include such things as the username, password, database, and server you wish to connect to. Some of the methods that will exist include such things as connect and disconnect. ❑ Command: This object is the one that carries information about what it is you want to do. Some object models will not include this object, or at least not feature it, but the concept is always there. (It is sometimes hidden as a method of the connection object.) ❑ Data set: This is the result of a query — that is, if the query returns data. Some queries in which you execute, for example, a simple INSERT statement, will not return results, but, if results are returned, there will be some sort of data set (sometimes called a result set or recordset) that the query returns them into. Data set objects will generally allow for you to iterate through the records in them (often forward only in direction but usually settable to allow for more robust positioning). They will also generally allow for data to be updated, inserted, and deleted. Connectivity Examples 650 What we’re going to do in this section is provide some hyper (and I do mean in the extreme) examples of how to get connected in the two most common languages of the day — C# and VB.NET. For each language, we’ll show a couple of examples — one for each of two different kinds of operations (fetching a simple data set and executing a query that does not return a data set).

Connecting in C# C# is a fairly clean language and is relatively easy to learn, much like VB, but it has the extra benefit of providing some C-based concepts and also being much closer in syntax (making it easier to transition between the two). Returning a Data Set using System; using System.Data.SqlClient; class Program { static void Main() { //Create some base strings so you can look at these // separately from the commands they run in // Integrated Security - next line should be uncommented to use string strConnect = “Data Source=(local);Initial Catalog=master;Integrated Security=SSPI”; // SQL Security - next line should be uncommented to use //string strConnect = “Data Source=(local);Initial Catalog=master;User Id=sa;Password=MyPass”; string strCommand = “SELECT Name, database_id as ID FROM sys.databases”; SqlDataReader rsMyRS = null; SqlConnection cnMyConn = new SqlConnection(strConnect); try { } Appendix B: Very Simple Connectivity Examples // “Open” the connection (this is the first time it actually // contacts the database server) cnMyConn.Open(); // Create the command object now SqlCommand sqlMyCommand = new SqlCommand(strCommand, cnMyConn); // Create the result set rsMyRS = sqlMyCommand.ExecuteReader(); //Output what we got while (rsMyRS.Read()) { // Write out the first (ordinal numbers here) // column. We can also refer to the column by name Console.WriteLine(rsMyRS[“Name”]); } Console.WriteLine(); Console.WriteLine(“Press any key to continue...”); Console.ReadKey(); 651

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!