简体   繁体   中英

How to handle SqlDatareader on N-tier application?

I am confuse where can i make my code to n-tier: While learning n-tier i know now how to insert,delete,update. But now i am confused how to deal with sqldatareader to bind data on listbox and combo box:

This code works on my presentation layer but dont know how to convert it to layers as DataAccess,BusinessObject,BusinessLogic.

FormLoad
{
getlistview();
cboStatus();
}

#region "fill listview"
public void GetlistView()
{
        int i = 0;

        SqlConnection sqlcon = new SqlConnection(connStr);
       lstBF.Items.Clear();
        SqlCommand sqlcom = new SqlCommand("sp_LoadNew", sqlcon);
        SqlDataReader dr;
        lstBF.Items.Clear();
        sqlcon.Open();
        dr = sqlcom.ExecuteReader();

        while (dr.Read())
        {
            lstBF.Items.Add(dr["SerialNumber"].ToString());
            lstBF.Items[i].SubItems.Add(dr["PartNumber"].ToString());
            lstBF.Items[i].SubItems.Add(dr["StatusDescription"].ToString());
            lstBF.Items[i].SubItems.Add(dr["CustomerName"].ToString());
            lstBF.Items[i].SubItems.Add(dr["DateCreated"].ToString());
            lstBF.Items[i].SubItems.Add(dr["CreatedBy"].ToString());
            lstBF.Items[i].SubItems.Add(dr["ModifiedBy"].ToString());

            i = i + 1;
        }

        if (sqlcon.State == ConnectionState.Open) sqlcon.Close();
    }
    #endregion

    #region "ListviewChange"
    private void lstBF_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lstBF.SelectedItems.Count == 1)
        {
          txtSerialNumber.Text = lstBF.SelectedItems[0].Text;
           txtPartNumber.Text  = lstBF.SelectedItems[0].SubItems[1].Text;
           lblStatus.Text  = lstBF.SelectedItems[0].SubItems[2].Text;
          lblcustomer.Text = lstBF.SelectedItems[0].SubItems[3].Text;
         lblModifiedBy.Text  = lstBF.SelectedItems[0].SubItems[6].Text;
        }
    }
    #endregion

    #region "FILL combo"
    public void cboStatus()
    {
        try
        {
          SqlConnection conn = new SqlConnection(connStr);
            SqlCommand sqlcom = new SqlCommand("sp_loadStatus",conn);
            SqlDataReader dr = null;
            conn.Open();
            dr = sqlcom.ExecuteReader();
            cmbStatus.Items.Clear();

            while (dr.Read())
            {
                 cmbStatus.Items.Add((dr["StatusDescription"]));
            }

            if (conn.State == ConnectionState.Open) conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error Occurred:" + ex);
        }
        finally
        {
        }
    }
    #endregion

If you want to have a nice, clean separation, here's what you should do:

  • never ever pass something like a SqlDataReader or any other database-dependant object up from your data layer - encapsulate everything in your data layer, and from there on up, use your own domain model (classes)

  • the data layer should turn your database requests into objects of your domain model. You can definitely do that by hand - but it's a lot of boring and error prone code to do all the DataReader, read each row, convert to object kind of stuff - here, a tool called an OR mapper (object-relational mapper) can help tremendously, since it does all of this for you - more or less for free. Check out SubSonic , Linq-to-SQL and quite a few more out there.

  • for things like combobox lookup lists, you would typically design a "view model", eg a class for that "view" (or webform, or winform) that will hold the data that this view is supposed to a) show, and b) needs for its job. Typically, such a "view model" is just another class - no magic about it. It will contain one or several of your domain model classes (the actual data you want to show), and one or several lookup lists that contain the possible values for all the dropdowns etc.

With this approach, you should be fine and well on track to a good solid design, and by using an ORM, you can save yourself a ton of boring code and concentrate on the more interesting parts of your app.

Update:
Sample for binding your combo box:

  • create a class for your lookup values, typically something like:

     public class StatusCode { public int ID { get; set; } public string Description { get; set; } } 
  • have a method in your data layer to retrieve all values from your StatusCode table into a List<StatusCode>

     public List<StatusCode> GetAllStatusCodes(); 
  • have your combo box in the UI bound to that list:

     cbxStatusCode.DataSource = statusCodeList; cbxStatusCode.DisplayMember = "Description"; cbxStatusCode.ValueMember = "ID"; 

    Note: this is slightly different depending on whether you use Winforms or ASP.NET webforms.

There you have it!

One place you could start is using the Entity Framework or a class generator like Subsonic.

watch this podcast, follow it through and have a look at the code it creates for you:

http://www.techscreencast.com/language/dotnet/subsonic-getting-started-webcast/227

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM