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

4th attempt: much better.

       My idea is to incorporate another tier (hooray for n-tier architecture) to separate presentation layer and DAL. Let’s create DataBroker class.


using System;

namespace LesioS
{
 public class DataBroker
 {
  private string _settingName;
  private string _settingValue;

  public DataBroker(string settingName, string settingValue)
  {
   _settingName = settingName;
   _settingValue = settingValue;
  }

  public string SettingName
  {
   get { return _settingName; }
  }

  public string SettingValue
  {
   get { return _settingValue; }
  }
 }
}


       It’s extremely simple class with 1 constructor and 2 properties for reading data. Now let’s rewrite LoadSetting function.


public static ArrayList 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);
 ArrayList settings = new ArrayList();
 while(dr.Read())
  settings.Add(new DataBroker(dr["SettingName"].ToString(), dr["ValueId"].ToString()));
 dr.Close();
 return settings;
}


       And btnLoad_Click function.


private void btnLoad_Click(object sender, System.EventArgs e)
{
 ArrayList settings = DAL.LoadSetting(txtNumber.Text);
 DataBroker db = (DataBroker)settings[0];
 txtSetting.Text = db.SettingName;
 ddlValue.SelectedValue = db.SettingValue;
}


       The most important thing is that using System.Data.SqlClient clause disappeared from code-behind again and using System.Collection clause appeared, but from porting point of view it doesn’t matter. Loading arise from creating ArrayList can be omitted.
       On the end I’ve to mention that code presented in LoadSetting function is not what I use in development; it was only for presentation reason. For everyday work I use Microsoft.ApplicationBlocks.Data component and I invite to become familiar with this solution.


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-23 9:18:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search