Exposing data from SQL

Discussion in 'C#' started by nubsii, May 9, 2008.

  1. #1
    Whats the real way to expose data from a Data Access Layer?

    I've gotten into the habit of making my own wrappers for data. For example I just finished an application which displays 10 article titles + short descriptions in a table and has a paginated link structure to browse all of the articles in my database. To do this I created a class called ArticleModel and it basically consists of 3 strings which I've exposed as fields, Author, ArticleTitle and ArticleText. I have a DataAccessLayer.GetArticles(int articleToStartAt, int numberOfArticlesToGet) and it returns an arraylist of ArticleModels. Yea everything works fine but I don't wanna do things this way anymore!!

    Is there an equivalent to a DataBaseResource from php? How does your code expose data for usage in you web app? I read about DataSets and DataTables, and I'd just like to know what other people do in their code. A code example where you run a query and then do ____ to get it into a dataset/other and return dataset/other; would be much appreciated!

    My DataAccess.GetSomething() code always ends up looking like this..
    
      //sql query occurs and then...
      Arraylist myStuff = new Arraylist();
      while (reader.Read())
      {
         myStuff.Add(new MyMadeUpClass(reader["col1"], reader["col2"]));
      }
      return myStuff;
    
    Code (markup):
    Thanks for reading.
     
    nubsii, May 9, 2008 IP
  2. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #2
    I generally use DataTables.

    
    
    DataTable ret = new DataTable();
    
    try {
         connection.Open();
    
         reader = cmd.ExecuteReader();
    
         ret.Load(reader);
    }
    catch(...)
    {
    
    }
    finally
    {
    
    }
    
    return ret;
    
    
    
    Code (markup):
     
    dgxshiny, May 10, 2008 IP
  3. nubsii

    nubsii Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cool, thank you dgxshiny.

    Looks like im not allowed to give you any more rep for awhile, so you're gonna have to stop answer my questions :p j/k
     
    nubsii, May 12, 2008 IP