12.07.2015 Views

net developer's journal sneak peek - sys-con.com's archive of ...

net developer's journal sneak peek - sys-con.com's archive of ...

net developer's journal sneak peek - sys-con.com's archive of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FeatureAll Data, All the Time…An introduction to .NET data accessand SQLXML Web servicesSERVERBY BOB BEAUCHEMINHOMEDESKTOPEVERYWHEREin this “Data Access” column,I will discuss topicsrelating to the Micros<strong>of</strong>t.NET data access stack,known as ADO.NET. This installmentstarts with an overview <strong>of</strong> thedifferent pieces <strong>of</strong> the stack and finisheswith an interesting developmentin the world <strong>of</strong> SQL Server,.NET, and XML Web services.The ADO.NET data access APIs<strong>con</strong>sist <strong>of</strong> three types <strong>of</strong> classes:1. Classes for executing databasecommands in <strong>con</strong>nected mode:This is accomplished by a series<strong>of</strong> data providers that run inmanaged code as much as possible.Each <strong>of</strong> these data providersimplements approximately thesame set <strong>of</strong> interfaces andexposes an analogous class hierarchy.2. Dis<strong>con</strong>nected data access classes:A series <strong>of</strong> classes in theSystem.Data namespace that<strong>con</strong>sists <strong>of</strong> a complex in-memorycollection class (called theDataSet), which approximatesrelational database semantics,and supporting classes such asthe DataTable, DataRow, andDataRelation.3. Classes for XML integration:DataSets, with help from a fewadditional classes in the System.Xmlnamespace (althoughthey “live” in the System.Data.dll),integrate the XML and relationaldata access worlds. It’s also possibleto allow for data interchangewith heterogeneous platforms,such as Unix <strong>sys</strong>tems and IBMmainframes, using XML Web services.An ADO.NET Crash CourseWe’ll start with a crash course inADO.NET <strong>con</strong>sisting <strong>of</strong> some simpleprograms.Executing Database Commandsin Connected ModeData providers include a SQLServer–specific provider (theSqlClient data provider) and adata provider, known as the OleDbdata provider, which is a bridgefor OLE DB data sources. Justbefore .NET shipped in February,Micros<strong>of</strong>t released a bridgeprovider for ODBC data sourcesfor download from the Web. So, ifyou use SQL Server or have anOLE DB provider or ODBC driverfor your database, you can use the.NET data access classes directlywith your data.The simplest starter program forreading a resultset (the results <strong>of</strong> aSQL SELECT statement) <strong>con</strong>tainingtwo columns from a SQL Server table(in C#) would look like Listing 1.The program uses the SqlClientdata provider’s Connection class(which includes a <strong>con</strong>structor thattakes a database <strong>con</strong>nection string)to open a <strong>con</strong>nection to SQL Server.The Command class encapsulatesSQL statement functionality andincludes a <strong>con</strong>structor that takes aSQL command string. We use this toexecute the SQL; the results arereturned in a DataReader class. Thisclass has methods that allow us tomove forward through the rows inthe resultset, along with methodsthat allow us to access the data ineach row by using zero-based columnordinals.We’ll discuss using dataproviders with different datasources in more detail in futurecolumns. In addition to Micros<strong>of</strong>t’sdata providers, third parties canbuild data providers. For example,DataDirect Technologies hasshipped native providers for Oracleand Sybase. You can also build yourown. I built a simple data providerthat’s available for download athttp://staff.develop.com/bobb.Dis<strong>con</strong>nected Dataand the DataSet ClassThe DataSet class is a specializedcollection class that includes a collectionthat follows the relationaldata model. If you’re used to dealingwith tables, columns, rows, andrelationships, you’ll be right athome with DataSet. DataSets can beused as an in-memory cache or adata source for ASP.NET Web Forms<strong>con</strong>trols, or they can be directlybound and manipulated byWindows Forms <strong>con</strong>trols. DataSetsinteract with data providers througha data provider’s DataAdapter class.Listing 2 shows a simple programthat reads data into a DataSet,updates the DataSet, and flushesthe updates back to the database.Note that you don’t have to open the<strong>con</strong>nection directly when you’reusing a DataAdapter because theDataAdapter.Fill method opens the<strong>con</strong>nection, reads the rows, andthen closes the <strong>con</strong>nection automatically.Although using a DataSet is similarin <strong>con</strong>cept to using the dis<strong>con</strong>nectedADO Recordset class, aDataSet can <strong>con</strong>tain multiple tables16Preview Issue • dot<strong>net</strong>developers<strong>journal</strong>.com

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

Saved successfully!

Ooh no, something went wrong!