Daniel
Daniel

Reputation: 125

Is it possible to fill a normal array with values from the sql server?

I want to create a method that returns an array of my class i know how to do with arrayList but i need to fill a 'normal' array[].

I'm doing this with arrayList

public ArrayList GetListProductFamily()
{         
    ArrayList arrayProductFamily = new ArrayList();

    IConnection conn = ConnUtil.GetConnection();
    string query = GET_QUERY;

    ProductFamily productFamily = new ProductFamily();

    try
    {
        conn.ExecuteReader(query);

        if (conn.DataReader.Read())
        {
            productFamily.cIdProductFamily = DB.LoadString(conn.DataReader, "CIDPRODUCTFAMILY");
            productFamily.cProductFamily = DB.LoadString(conn.DataReader, "CPRODUCTFAMILY");
            productFamily.lActive = Convert.ToInt32(DB.LoadInt(conn.DataReader, "LACTIVE"));

            arrayProductFamily.Add(productFamily);
        }
    }
    catch (Exception ex)
    {
        Logger.Error("Error", ex);
    }

    return arrayProductFamily;
}

Upvotes: 0

Views: 132

Answers (2)

Nazmul
Nazmul

Reputation: 595

You can do it by changing your code following way:

//ArrayList arrayProductFamily = new ArrayList();
ProductFamily[] arrayProductFamily = new ProductFamily[1];

But this will make you problem because above array is fixed length. You are using ExecuteReader so you can not count total number of records. So you need to change the length dynamically. You can do it using Array.Resize, your code may be change like following

    while (dataReader.Read())
    { 
      Array.Resize(ref arrayProductFamily , arrayProductFamily.Length+1);

.... rest of your code

    }

Upvotes: 0

Ron Beyer
Ron Beyer

Reputation: 11273

public ProductFamily[] GetListProductFamily()
{
    List<ProductFamily> arrayProductFamily = List<ProductFamily>();

    IConnection conn = ConnUtil.GetConnection();
    string query = GET_QUERY;


    try
    {
        var dataReader = conn.ExecuteReader(query);

        while (dataReader.Read())
        {
            ProductFamily productFamily = new ProductFamily();
            productFamily.cIdProductFamily = DB.LoadString(dataReader, "CIDPRODUCTFAMILY");
            productFamily.cProductFamily = DB.LoadString(dataReader, "CPRODUCTFAMILY");
            productFamily.lActive = Convert.ToInt32(DB.LoadInt(dataReader, "LACTIVE"));

            arrayProductFamily.Add(productFamily);
        }
    }
    catch (Exception ex)
    {
        Logger.Error("Error", ex);
    }

    return arrayProductFamily.ToArray();
}

Its pretty simple, just replace ArrayList in your class with List<ProductFamily> and then return the .ToArray() for the list.

The use of ArrayList is generally depreciated since the introduction of Generics, and you should avoid it as much as possible over the use of List<T> with strong typing.

You were also not iterating through your reader, and I'm not sure what DB.LoadXXX does, but I'm assuming it takes a IDbDataReader and loads the specified column name. These can be replaced with dataReader.GetInt32 or dataReader.GetString, depending on what you want to read.

It should also be noted that you want to check for DBNull, otherwise you will throw an exception early and exit out. You probably want to move the try/catch inside the while loop so that one single record doesn't cause the reader to fail.

You also want to use the reader in a using loop, but I'll leave that up to you to implement.

Upvotes: 1

Related Questions