Maria Agaci
Maria Agaci

Reputation: 281

Convert sql select result to c# object

i want to create a c# object dynamically with data from sql select query

Query result example:

Percentage  Way

    47.64   DPAD_UP
    35.20   DPAD_DOWN
    8.67    LEFT_RIGHT_MENU
    5.54    MENU
    2.88    MENU_KRYESORE
    0.06    EPG_CLICK

from this query i want to create te following object:

data = new object[] { 
    new object[] { "DPAD_UP", 47.64 }, 
    new object[] { "DPAD_DOWN", 35.20 }, 
    new object[] { "LEFT_RIGHT_MENU", 8.67 },
    new object[] { "MENU", 5.54 }, 
    new object[] { "MENU_KRYESORE", 2.88 }, 
    new object[] { "EPG_CLICK", 0.06 } 
}

How to do this?

Upvotes: 0

Views: 10641

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Are you looking for something like that? Just direct reading (I've assumed that you use MS SQL DBMS)

      List<Object[]> results = new List<Object[]>();

      using (SqlConnection conn = new SqlConnection("Your connection String")) {
        conn.Open();

        using (SqlCommand query = new SqlCommand("your query", conn)) {
          //TODO: May be you have parameters - assign them here...

          using (var reader = query.ExecuteReader()) {
            while (reader.Read()) {
              results.Add(new Object[] {reader.GetValue(0), reader.GetValue(1)});
            }
          }
        }
      }

      data = results.ToArray(); 

Upvotes: 3

Related Questions