locked
C# datatable into int Array RRS feed

  • Question

  • User1324169804 posted

    How to turn SQL Datatable into int Array. Here is my table

    ID FName LName Age
    1 test abcd 12
    1 test abcd 25
    1 test abcd 16
    2 Name Last 12
    3 adfasdfasd asfasdfasdf 14
    3 adfasdfasd asfasdfasdf 1
    3 adfasdfasd asfasdfasdf 6
    6 sdfasdf sdfasdf 10

    How to get all Age value into one int array?

    Here is my code:

                    string query = "SELECT * FROM Table_1";
                    SqlDataAdapter SDA = new SqlDataAdapter(query, con);
                    DataTable dt = new DataTable();
    
                   DataSet ds = new DataSet();
                    SDA.Fill(ds, "Table_1");
                    dt = ds.Tables["Table_1"];
    
                    
                  int columnIndex = 3; 
    
                    int[] results =  null;
                    results = new int[dt.Rows.Count];
                    int n = results.Length;
                    for (int index = 0; index < dt.Rows.Count; index++)
                    {
                        results[index] = (int)dt.Rows[index][columnIndex];           
    }

    Thursday, May 30, 2019 9:52 PM

All replies