Implementing ODBC in ASP .NET

The source code

Dataset: dsUser.xsd

In .NET, a Dataset begins with an XML schema. This is the magic of .NET. Any data source, once mapped onto an XML schema, can be manipulated by the same code. This is the easy part, because Visual Studio .NET includes a graphical XML schema editor. Figure 1 illustrates this Dataset's design. The schema should reflect the relevant columns in the table or data view under consideration.


Figure 1 – The dsUser XML Schema

Code Behind: news.aspx.cs

The source file for the News & Updates page is named news.aspx. This file contains the HTML and scripting that define the page. The code behind the page, which defines the namespace hotquant.News, is located in news.aspx.cs. HotQuant’s web site is standardized on C# .NET. Readers who are more famliar with Visual Basic .NET should have little trouble following along.

// This is the assembly containing Microsoft's ODBC data objects,
// which are available for download here. You must also include this
// assembly in the list of project references.
using Microsoft.Data.Odbc;

// This is HotQuant's application namespace.
namespace hotquant
{
 // This is the class defining the News & Updates page.
 public class News : System.Web.UI.Page
 {
   // These are the server controls added in the design view.
   protected System.Web.UI.WebControls.TextBox txtName;
   protected System.Web.UI.WebControls.TextBox txtCompany;
   protected System.Web.UI.WebControls.TextBox txtPosition;
   protected System.Web.UI.WebControls.TextBox txtEmail;
   protected System.Web.UI.WebControls.RequiredFieldValidator
     rfvEmail;
   protected System.Web.UI.WebControls.RegularExpressionValidator
     revEmail;
   protected System.Web.UI.WebControls.Button btnSubscribe;
   protected System.Web.UI.WebControls.Button btnUnsubscribe;
   protected ResourceManager rmData =
       new ResourceManager("hotquant.News", typeof (News).Assembly);

   // And now the ODBC data objects.
   // First the Connectuion object...
   protected OdbcConnection ocHQData = new OdbcConnection();

   // Then the Data Adapter...
   protected OdbcDataAdapter odaUser = new OdbcDataAdapter();

   // The Command Builder object...
   protected OdbcCommandBuilder ocbUser;

   // The Select Command objects...
   protected OdbcCommand cmdUserSelect = new OdbcCommand();

   // ... and finally the Dataset. This is why we're here.
   protected hotquant.data.dsUser userData =
     new hotquant.data.dsUser();

   // The data objects initialization code goes into the Page Load
   // function. This code provides the functionality that the ODBC
   // server controls WOULD provide if they functioned properly.
   private void Page_Load(object sender, System.EventArgs e)
   {
     // Set the connection string. In this case, the connection
     // is stored in a static string in the Global object, which
     // it retrieved from a resource file.
     this.ocHQData.ConnectionString = Global.ConnStr;

     // Set up the Select command and attach it to the Data Adapter.
     this.cmdUserSelect.Connection = this.ocHQData;
     this.cmdUserSelect.CommandText =
       "SELECT Email, Active, Name, Position, Company " +
       "FROM User " +
       "WHERE Email='" + this.txtEmail.Text + "'";  
     this.odaUser.SelectCommand = this.cmdUserSelect;

     // Initialize the Command Builder object.
     this.ocbUser = new OdbcCommandBuilder(this.odaUser);

     // Define table mappings within the Data Adapter. This is how
     // it knows which fields in the database (the Connection
     // object) correspond to which fields in the XML schema (the
     // Dataset).
     this.odaUser.TableMappings.AddRange(
       new System.Data.Common.DataTableMapping[]
     {
       new System.Data.Common.DataTableMapping("Table", "User",
         new System.Data.Common.DataColumnMapping[]
       {
         new System.Data.Common.DataColumnMapping("Email",
           "Email"),
         new System.Data.Common.DataColumnMapping("Active",
           "Active"),
         new System.Data.Common.DataColumnMapping("Name",
           "Name"),
         new System.Data.Common.DataColumnMapping("Position",
           "Position"),
         new System.Data.Common.DataColumnMapping("Company",
           "Company"),
       }) // Note the alternating parentheses & curly braces.
     });  // This is easy to screw up if you aren't paying
          // attention!

     // Now we connect the Dataset object we defined in the
     // declarations above to the XML schema file in our data
     // directory.
     this.userData.DataSetName = "dsUser";

     // This setting determines which character set is used to
     // perform alphanumeric indexing on the data. In other words,
     // it identifies the local alphabet.
     this.userData.Locale =
       new System.Globalization.CultureInfo("en-US");

   } // End of Page_Load function.

 } // End of News class.

}// End of hotquant namespace.

You might also like...

Comments

About the author

Jason G. Williscroft

Jason G. Williscroft United States

Jason Williscroft is a former Marine and naval officer, and a graduate of the United States Naval Academy, where he studied Systems Engineering. He is currently General Manager of HotQuant, LLC,...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debuggers don't remove bugs. They only show them in slow motion.”