Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 638 and 639: Appendix A: System Functions Cert_I
- Page 640 and 641: Appendix A: System Functions Encryp
- Page 642 and 643: Appendix A: System Functions @@FETC
- Page 644 and 645: Appendix A: System Functions CURREN
- Page 646 and 647: Appendix A: System Functions SYSDAT
- Page 648 and 649: Appendix A: System Functions GetRoo
- Page 650 and 651: Appendix A: System Functions The ex
- Page 652 and 653: Appendix A: System Functions RAND T
- Page 654 and 655: Appendix A: System Functions ❑ IN
- Page 656 and 657: Appendix A: System Functions ❑ Is
- Page 658 and 659: Appendix A: System Functions FILEGR
- Page 660 and 661: Appendix A: System Functions INDEX_
- Page 662 and 663: IsCheckCnst IsConstraint IsDefault
- Page 664 and 665: Appendix A: System Functions OBJECT
- Page 666 and 667: Appendix A: System Functions TYPEPR
- Page 668 and 669: Appendix A: System Functions The qu
- Page 670 and 671: Appendix A: System Functions The SU
- Page 672 and 673: Appendix A: System Functions ASCII
- Page 674 and 675: Appendix A: System Functions The ch
- Page 676 and 677: Appendix A: System Functions UNICOD
- Page 678 and 679: Appendix A: System Functions CAST a
- Page 680 and 681: Appendix A: System Functions HOST_I
- Page 682 and 683: Appendix A: System Functions NULLIF
- Page 684 and 685: Appendix A: System Functions 646 Pr
- Page 686 and 687: Appendix A: System Functions Text a
- Page 690 and 691: Appendix B: Very Simple Connectivit
- Page 692 and 693: Appendix B: Very Simple Connectivit
- Page 694 and 695: Appendix B: Very Simple Connectivit
- Page 696 and 697: ABS function A ABS function, 611 AC
- Page 698 and 699: closing tags closing tags, XML docu
- Page 700 and 701: cryptographic functions (continued)
- Page 702 and 703: defects defects. See well formed XM
- Page 704 and 705: EXPLICIT option EXPLICIT option bas
- Page 706 and 707: IGNORE_DUP_KEY option (CREATE INDEX
- Page 708 and 709: LOWER function LOWER function, 635
- Page 710 and 711: online transaction-processing (OLTP
- Page 712 and 713: query results query results alphabe
- Page 714 and 715: scripts (continued) creating with M
- Page 716 and 717: state state, basics of, 391 stateme
- Page 718 and 719: transactions (continued) COMMIT TRA
- Page 720: View Builder View Builder (Manageme
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).