Porting web application to different database server
page 4 of 5
by Lech P. Szczecinski
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21863/ 48

3rd attempt: better and worse.

       Of course it’s rather simply to find something which is lighter that DataSets and which provides us data from database: data readers. They are light, forward-only, but they have one disadvantage: they’re connected with data provider which are specific to databases. So it means that using System.Data.SqlClient clause will appear in code-behind again.
       We can use one data reader which is common to all databases. It’s OleDbDataReader, but using it bereave us efficiency which comes from specific to database data reader. So it’s not that way.
       Let’s incorporate data readers first. Take a look at changed DAL class (in SaveSetting function we’ve made no changes):


public class DAL
{
 public static SqlDataReader LoadDllValues()
 {
  SqlConnection connection = new SqlConnection(ConnectionString.Text);
  string selectCommand = "SELECT * FROM [Values] ORDER BY ValueName";
  SqlCommand sqlCommand = new SqlCommand(selectCommand, connection);
  connection.Open();
  SqlDataReader dr = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
  return dr;
 }

 public static SqlDataReader LoadSetting(string settingId)
 {
  SqlConnection connection = new SqlConnection(ConnectionString.Text);
  string selectCommand = "SELECT * FROM Settings1 WHERE SettingId = @SettingId";
  SqlCommand sqlCommand = new SqlCommand(selectCommand, connection);
  sqlCommand.Parameters.Add(new SqlParameter("@SettingId", SqlDbType.Int, 4));
  sqlCommand.Parameters["@SettingId"].Value = settingId;
  connection.Open();
  SqlDataReader dr = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
  return dr;
 }
}


       Both functions returns SqlDataReader object. In code-behind we have following changes:


using System.Data.SqlClient;
private void btnLoad_Click(object sender, System.EventArgs e)
{
 SqlDataReader dr = DAL.LoadSetting(txtNumber.Text);
 dr.Read();
 txtSetting.Text = dr["SettingName"].ToString();
 ddlValue.SelectedValue = dr["ValueId"].ToString();
}


       Nevertheless changing returning object type in LoadDllValues function code-behind didn’t change in this point: there was no need to declare object which takes data for dropdown list. Different situation is with btnLoad_Click function: cause we can’t load separate textboxes from data reader we’ve to declare object, load data into and then assign proper data to proper textbox. Unfortunately using System.Data.SqlClient clause appeared again.
       Fortunately we have more elegant solution in btnLoad_Click function and, what is much more important, we have lighter solution what have significant meaning with heavy loaded web servers. So we may suppose that solution goes to right direction, but we have again problem with porting application to another database server.


View Entire Article

User Comments

Title: N-tier Porting   
Name: Ben Gik
Date: 2012-02-05 5:34:44 PM
Comment:
Hi,
You made me understand porting and I really learn some tricks. Good programmers. Keep it up.
Title: Great but...   
Name: Omar
Date: 2007-05-01 11:26:22 AM
Comment:
The article is great in explaining the n-tier architecture, however, does not describe porting to just another server (how do I change the web.config file for a different server, what other considerations to take) and the grammar is really bad.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 4:30:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search