Hi @rahul kumar , Welcome to Microsoft Q&A,
Essentially, what you query is a datatable, and you can do all the operations that a datatable can do on it. as follows:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
DataTable dt = new DataTable();
dt = connection.FillDataGridView($"SELECT * FROM nameswithbalance").Tables[0];
dt.TableName = "originaltable";
List<Dictionary<string, object>> data = DataTableToDictionaryList(dt);
// Now 'data' contains the data from the "nameswithbalance" table in a list of dictionaries.
}
// Convert a DataTable to a List of Dictionaries
static List<Dictionary<string, object>> DataTableToDictionaryList(DataTable dt)
{
List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
foreach (DataRow row in dt.Rows)
{
Dictionary<string, object> rowData = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
rowData[col.ColumnName] = row[col];
}
dataList.Add(rowData);
}
return dataList;
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.