CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Posts
    109

    Table data into Array

    I wish to populate an array with the data from a table. How do I do this?
    More specifically, I wish to put the data from a specific column, into the array, and later on add all the values in each cell of the array.

    I tried searching on google but I guess I'm using the wrong words to search.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Table data into Array

    datareader would be one method.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Table data into Array

    DataTable will provide you a Row collection, and each row will contain the fields selected in your query. So you would access a value using:

    Code:
    string value = dtResults.Rows[0]["FIELDNAME"].ToString();
    Code:
                DataTable dtResults = new DataTable();
    
                OleDbConnection dbConnection = new OleDbConnection("your connection string");
    
                try
                {
                    dbConnection.Open();
    
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        OleDbCommand dbCommand = dbConnection.CreateCommand();
                        dbCommand.CommandType = CommandType.Text;
                        dbCommand.CommandText = "your query (select * from...)";
    
                        OleDbDataReader dbReader = dbCommand.ExecuteReader();
    
                        if (dbReader.HasRows)
                            dtResults.Load(dbReader);
    
                        dbReader.Close();
                        dbConnection.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Unable to execute query as requested.", ex);
                }

  4. #4
    Join Date
    Jul 2011
    Posts
    9

    Re: Table data into Array

    Hello

    This can also help you, try this

    Dim rst As DAO.Recordset
    Dim lngNum As Long

    Set rst = CurrentDb.Open("TableNameHere")

    For 1 to UBound(YourArray)
    With rst
    .AddNew
    !FieldName = YourArray(lngNum)
    .Update
    End With
    lngNum = lngNum + 1
    Next


    There may be error, not tested, just an overview.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Table data into Array

    Thanks for the effort. But, that code is for VB6. This is the C# Forum. Also, that code uses DAO. In .NET ADO is most common...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured