1

I'm with the follow question: I have a method that returns a SqlDataReader, then I do a While in this SqlDataReader as you see bellow:

using (SqlDataReader objSqlDtReader = objDtAccess.GetDataReader()) 
{
    while(objSqlDtReader.Read()) 
    {
           UserEntitie objUserEntitie = new UserEntitie();
           objUserEntitie.Name = Convert.ToString(objSqlDtReader["name"])    
    }
}

So, I need to fill all my UserEntite with all data, Name, Email, Id. But I have many methods in User Class as (GetUserById, GetUserByEmai, GetAllUsers), and I have to fill a data again.

I need to create an private method that fill my UserEntitie and in each method I only call my method FillUser and return its. I think that I need to pass my SqlDataReader to my private method FillUser.

Any suggestions to do this?

1
  • 1
    Cant get where is the problem. You already decided what to do. Commented May 30, 2012 at 12:27

2 Answers 2

2

You could do something like this:

class UserEntitie
{
    internal void FillFromSqlDataReader(SqlDataReader reader)
    {
        this.Name = Convert.ToString(reader["name"]);
        /* Fill your fields */
    }
}

Usage:

using (SqlDataReader objSqlDtReader = objDtAccess.GetDataReader()) 
{
    while(objSqlDtReader.Read()) 
    {
        UserEntitie objUserEntitie = new UserEntitie();
        objUserEntitie.FillFromSqlDataReader(objSqlDtReader);
        /* Add to a list or something */    
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm using a similar sort of system, but instead of passing the SqlDataReader into the private object, I instead pass a SqlConnection into the object and have a private FillDetailsFromDb() method which performs the necessary work.

So, instead of passing an SqlDataReader, I'll have a parameterless constructor as well as a constructor which takes a SqlConnection as a parameter.

When I have created the object, I'll pass a parameter (in my case its a Guid) into the object and my private method uses the SqlConnection it was constructed with along with a fresh SqlCommand which takes the parameter I sent to the public method.

    private void FillDetailsFromDb(Guid p)
    {
        SqlCommand com = new SqlCommand();
        com.Connection = conn;

        com.CommandText = "spSelectProspectUsingLinkParameter";
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@linkparam", p);
        conn.Open();
        SqlDataReader rdr = com.ExecuteReader();
        ...
        etc
    }

This means the object can still exist as a discrete object and can be used in other places and the SqlDataReader doesn't have to be passed around, which I've found it doesn't like very much.

Incidentally, while I was exploring this problem myself, I found there was a good chance I would strike weird null values in translating the data from the datatypes in the database to the .Net datatypes. Using something like the following code, I managed to avoid the DBNull datatypes as well as utilise the column names in the database.

if (!rdr.IsDBNull(rdr.GetOrdinal("firstname")))
{
    Firstname = rdr.GetString(rdr.GetOrdinal("firstname"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.