How to execute a query and display the result query data?
Last modified:
Note: The below-given sample can be used apart from the Active Query Builder component in any .NET project.
The main result which you get using Active Query Builder is the SQL query text, and you aren't limited to specific ways to execute queries or display the result query data. But Active Query Builder can help you to cope with this task.
The component is shipped with various demo projects where you can find different code samples, including the Query Results demo. It provides code samples for executing SQL queries and using various controls to display data. Below are some code samples from this project.
Note: The demo projects are installed to the "%USERPROFILE%\Documents\Active Query Builder 3 ASP.NET Examples" folder.
We've made simple static helpers methods that execute arbitrary SQL query and save the result to a simple list (rows) of string lists (columns). There are two modifications of them: the first variant (GetData) returns data with column names while the second returns only the data (GetDataList), you can use it when you already know which columns will be returned. The sample of their usage is below:
var qb = QueryBuilderStore.Get(); var conn = qb.SQLContext.MetadataProvider.Connection; var sql = qb.SQL; try { var data = DataBaseHelper.GetData(conn, sql); return Json(data, JsonRequestBehavior.AllowGet); } catch (Exception e) { return new HttpStatusCodeResult(500, e.Message); }
In the code of the Query Results demo, you can find the code for populating various types of grids (jqxGrid, js-Grid, React data grid) and controls (json Editor) with that data.
The full code of the 'DataBaseHelper.cs' file is below for your convenience. This code is not related to Active Query Builder, you can use and modify it at your sole discretion under the WTFPL license.
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.OleDb; using System.IO; using System.Web; using System.Data.SQLite; namespace MVC_Samples.Helpers { public static class DataBaseHelper { public static List<Dictionary<string, object>> GetData(IDbConnection conn, string sql) { IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; if (string.IsNullOrEmpty(sql)) return new List<Dictionary<string, object>>(); try { if (conn.State != ConnectionState.Open) conn.Open(); IDataReader reader = cmd.ExecuteReader(); return ConvertToList(reader); } finally { conn.Close(); } } private static List<Dictionary<string, object>> ConvertToList(IDataReader reader) { var result = new List<Dictionary<string, object>>(); while (reader.Read()) { var row = new Dictionary<string, object>(); for (int i = 0; i < reader.FieldCount; i++) row.Add(reader.GetName(i), reader[i]); result.Add(row); } return result; } public static List<List> GetDataList(IDbConnection conn, string sql) { IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; if (string.IsNullOrEmpty(sql)) return new List<List>(); try { if (conn.State != ConnectionState.Open) conn.Open(); IDataReader reader = cmd.ExecuteReader(); return Convert(reader); } finally { conn.Close(); } } private static List<List> Convert(IDataReader reader) { var result = new List<List>(); while (reader.Read()) { var row = new List(); for (int i = 0; i < reader.FieldCount; i++) row.Add(reader[i]); result.Add(row); } return result; } } }