The Common Language Runtime

The foundation on which the .NET Framework is built is the Common Language Runtime (CLR). The CLR is the execution environment that manages .NET code at run time. In some ways, it is comparable to the Java Virtual Machine (JVM), or to the Visual Basic 6 runtime (msvbvm60.dll). Like these, the .NET Framework needs to be installed on any machine where .NET programs will be run. Unlike these, however, the CLR was designed specifically to support code written in many different languages. It's true...

Data Table Events Example

In the following example we create a DataTable and populate it dynamically, as we did in the previous example. We will build an ASP.NET web form interface that will enable updating and deleting rows in the DataTable, which will cause each of the aforementioned events to fire and be handled. The code for this example is rather long, so we'll break down as we go. If you are building this sample, write all the code in the gray boxes continuously as we go through it. The web form we are building...

Formatting the Data Grid

As you can see from the figure above, the datagrid's default properties make the table look rather sparse. However, Visual Studio .NET provides two ways to improve the component's appearance The Property Builder wizard You can select these wizards by right-clicking on the datagrid component and choosing the respective menu item. The Auto Format wizard shows a list of templates to choose from After clicking the OK button, the layout and the schema will be applied to the datagrid immediately <...

Adding a Typed Dataset to the Project

Adding a DataSet object inside the Visual Studio .NET project is really easy. From the Project menu, select Add New Item and choose Data Set from the dialog box After choosing a valid filename and pressing the Open button, a new DataSet class will be generated automatically (this can be seen in Example5) public class dsAuthors System.Data.DataSet public dsAuthors() this.InitClass() private dsAuthors(SerializationInfo info, StreamingContext context) this.InitClass()...

Data Adapter Components

The DataAdapter transfers data from a data source to a dataset, and back again - a key ability of ADO.NET. Datasets allow us to work with data without remaining connected to the data source. Also, the same DataSet class can handle data retrieved from many different sources - SQL Server connections, OLE DB connections, XML, or the user interface. The DataAdapter enables the DataSet to update a database using SQL commands stored within the InsertCommand, UpdateCommand, and DeleteCommand...

Bringing it all Together

In this section we are going to be looking at an application that lets us browse the orders that are stored in the Northwind database. We'll use SQL, stored procedures, and the GetSchemaTable method to provide us with a simple and fast way of viewing details for a particular order. The two projects, SqlBrowser and OleDbBrowser, have the source code for this application. I won't be showing all the code in these pages simply because a lot of the code generated to create the GUI is of no concern...

Accessing the Data in a Type Safe Manner

We have learned a fair amount about datareaders. We know how to traverse single or multiple result sets and how we get a result sets contents via the indexer. So far we haven't explored using any other method of accessing data, like using the type-safe methods that all datareaders implement. This is what we'll look at now. As we saw earlier, indexers allow us to access columns in the datareader either using the column name or the column ordinal the zero-based position of the column in the table...

Using the Sql Parameter and OleDbParameter Classes

The .NET Data Providers include classes for creating parameter objects that can be added to a command's ParametersCollection. The parameters are created with a name, data type, input or output specification, and a value if applicable. Both the SqlParameter and OleDbParameter classes function in the same way however, the SqlParameters are passed to the SQL Server as named parameters and must map to parameter names in the stored procedures. Creating Parameterized T-SQL Queries We can create...

Executing Stored Procedures with a Data Reader

We have seen how to execute simple SQL statements against the database, so now we're going to look at how we use the command object and the datareader to execute a stored procedure. We'll be using the CustOrdersDetail stored procedure that comes with Northwind in this example. The stored procedure SQL is shown below don't worry too much about its contents, all we need to know is that it takes an order ID and returns the details for that order CREATE PROCEDURE CustOrdersDetail OrderID int AS...

Provider Specific Classes

The second major difference between ADO.NET and ADO is that there are specific sets of classes for each of the providers. In ADO, we simply used Connection, Command, and Recordset objects, regardless of the data source we were accessing if we changed the database system used say, upgraded from Access to SQL Server , then in theory at least all we needed to change was the connection string. Now we need to create a specific SqlConnection, OleDbConnection, etc. object, depending on the data...