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

2nd attempt: going better.

       Here comes a help from one of the ASP.NET basement: n-tier architecture. In 1st attempt we merged presentation layer and data access layer (DAL). So let’s try to separate them. Let’s create DAL class for communication with database:


using System;
using System.Data;
using System.Data.SqlClient;

namespace LesioS
{
 public class DAL
 {
  public static DataView LoadDllValues()
  {
   SqlConnection connection = new SqlConnection(ConnectionString.Text);
   string selectCommand = "SELECT * FROM [Values] ORDER BY ValueName";
   SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand, connection);
   DataSet ds = new DataSet();
   sqlDataAdapter.Fill(ds, "Values");
   return ds.Tables["Values"].DefaultView;
  }

  public static DataSet LoadSetting(string settingId)
  {
   SqlConnection connection = new SqlConnection(ConnectionString.Text);
   string selectCommand = "SELECT * FROM Settings WHERE SettingId = @SettingId";
   SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand, connection);
   sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@SettingId", SqlDbType.Int, 4));
   sqlDataAdapter.SelectCommand.Parameters["@SettingId"].Value = settingId;
   DataSet ds = new DataSet();
   sqlDataAdapter.Fill(ds, "Settings");
   return ds;
  }

  public static void SaveSetting(string settingId, string settingName, string settingValue)
  {
   SqlConnection connection = new SqlConnection(ConnectionString.Text);
   string updateCommand = "UPDATE Settings SET SettingName = @SettingName, ValueId = @ValueId WHERE SettingId = @SettingId";
   SqlCommand sqlCommand = new SqlCommand(updateCommand, connection);
   sqlCommand.Parameters.Add(new SqlParameter("@SettingName", SqlDbType.NVarChar, 10));
   sqlCommand.Parameters["@SettingName"].Value = settingName;
   sqlCommand.Parameters.Add(new SqlParameter("@ValueId", SqlDbType.Int, 4));
   sqlCommand.Parameters["@ValueId"].Value = settingValue;
   sqlCommand.Parameters.Add(new SqlParameter("@SettingId", SqlDbType.Int, 4));
   sqlCommand.Parameters["@SettingId"].Value = settingId;
   sqlCommand.Connection.Open();
   sqlCommand.ExecuteNonQuery();
   sqlCommand.Connection.Close();
  }
 }
}


       And page code-behind comes to:


private void Page_Load(object sender, System.EventArgs e)
{
 if(!Page.IsPostBack)
 {
  ddlValue.DataSource = DAL.LoadDllValues();
  ddlValue.DataBind();
 }
}

private void btnLoad_Click(object sender, System.EventArgs e)
{
 DataSet ds = DAL.LoadSetting(txtNumber.Text);
 txtSetting.Text = ds.Tables["Settings"].Rows[0]["SettingName"].ToString();
 ddlValue.SelectedValue = ds.Tables["Settings"].Rows[0]["ValueId"].ToString();
}

private void btnSave_Click(object sender, System.EventArgs e)
{
 DAL.SaveSetting(txtNumber.Text, txtSetting.Text, ddlValue.SelectedValue);
}


         Pay attention that using System.Data.SqlClient clause disappear from code. So it could be end of our work with preparing ready-to-porting code. Now you have not to touch presentation layer (in fact all .aspx pages) and the only one thing you have to change is DAL class to force to work with Oracle, Sybase or something else.
       End of work now comes mainly from fact that DataSets are independent from database used with application. It’s huge plus. Another benefit is that DataSets have a lot of functionality implemented inside. In the other hand, this functionality in 80% of cases is not used, cause there’s no such need. Minus is that DataSet is rather heavy class. Besides, take a look at btnLoad_Click function: filling textboxes with data is rather inelegant. Let’s try to find some more efficient way.


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-25 8:17:45 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search