ASP.Net 7 : Using ADO Helper Class in Minimal API/Controller

public class MyController : Controller
{
private readonly ADONetHelper _ado;

public MyController(ADONetHelper ado)
{
_ado = ado;
}

[HttpGet(“/a”)]
public async Task GetData()
{
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter(“MobileNo”, “1234”),
new SqlParameter(“FullNo”, “1234”),
new SqlParameter(“TZDiff”, “0”)
};
var jsonData = await _ado.GetDT_SP_Async2(“getHukamnama”, parameters);
return Ok(jsonData);
}
}

app.MapGet(“/a”, async (ADONetHelper ado) =>
{

SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter(“MobileNo”, “1234”),
new SqlParameter(“FullNo”, “1234”),
new SqlParameter(“TZDiff”, “0”)
};
var jsonData = await ado.GetDT_SP_Async2(“getHukamnama”, parameters);
return jsonData;
});

ADO.NET Helper Class

using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using System.Data;

namespace EkHukam_Gurudwara.Data
{

public class ADONetHelper
{
private readonly IConfiguration _config;

public ADONetHelper(IConfiguration config)
{
_config = config;
}

public string GetDefaultConnection()
{
return _config.GetConnectionString(“DefaultConnection”);
}
public async Task GetConnStr()
{
return await Task.FromResult(_config.GetConnectionString(“DefaultConnection”));
}

public async Task GetDT_SP_Async(string spName, SqlParameter[] parameters)
{
string connString = GetDefaultConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(spName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(parameters);
await conn.OpenAsync();
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
}
}
}

public async Task GetDT_SP_Async2(string spName, SqlParameter[] parameters)
{
string connString = GetDefaultConnection();
string json = “”;
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(spName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;

if (parameters != null)
{
cmd.Parameters.AddRange(parameters);
}

await conn.OpenAsync();
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
DataTable dt = new DataTable();
dt.Load(reader);
json = JsonConvert.SerializeObject(dt, Formatting.Indented);
return json;
}
}
}
}

public async Task GetTest()
{
return await Task.FromResult(“EkOnkar”);
}

public DataTable getDataTable(string ConnStr, string sql)
{
using (SqlConnection connection = new SqlConnection(ConnStr))
{
connection.Open();
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
}

public async Task getDataTableAsync(string ConnStr, string sql)
{
using (SqlConnection connection = new SqlConnection(ConnStr))
{
await connection.OpenAsync();
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = await command.ExecuteReaderAsync();
DataTable dataTable = new DataTable();
dataTable.Load(reader);
return dataTable;
}
}

}

}
}

Scroll to Top