SQLHelper is a simple class to help with running queries against a database.
In order to use the system, you do need register it with your ServiceCollection:
serviceCollection.AddCanisterModules();
This is required prior to using the SQLHelper class for the first time. Once Canister is set up, you can use the SQLHelper class:
var Configuration = new ConfigurationBuilder()
.AddInMemoryCollection()
.Build();
var StringBuilderPool = serviceCollection
.BuildServiceProvider()
.GetService<ObjectPool<StringBuilder>>();
var Instance = new SQLHelper(StringBuilderPool, Configuration);
Or simply ask for an instance using dependency injection:
public MyClass(SQLHelper helper) { ... }
The SQLHelper class takes in an ObjectPool<StringBuilder>, an IConfiguration, and an optional logger. Once an instance is set up, you can create a batch, add queries, and then execute them.
var Results = await Instance.CreateBatch()
.AddQuery(CommandType.Text,"SELECT * FROM [TestDatabase].[dbo].[TestTable]")
.AddQuery(CommandType.Text,"SELECT * FROM [TestDatabase].[dbo].[TestTable2]")
.AddQuery(CommandType.Text,"SELECT * FROM [TestDatabase].[dbo].[TestTable3]")
.ExecuteAsync();
The Results object then holds the results for all 3 queries and is returned as IList<IList>. So in order to get the results from the queries:
var FirstQueryResults = Results[0];
var SecondQueryResults = Results[1];
var ThirdQueryResults = Results[2];
It is also possible to convert the results from the dynamic type to a class type that you specify:
var TestTableClasses = FirstQueryResults.Select(x => (TestTableClass)x).ToList();
The type will be converted automatically for you with no special type conversion required. SQLHelper also has an ExecuteScalarAsync function:
var Result = await Instance.ExecuteScalarAsync<int>();
This will either return the first value of the first set of results OR it will return the number of rows that were effected depending on whether or not the query was a select or not.
The library is available via Nuget with the package name "SQLHelper.DB". To install it run the following command in the Package Manager Console:
Install-Package SQLHelper.DB
In order to build the library you will require the following as a minimum:
- Visual Studio 2022
Other than that, just clone the project and you should be able to load the solution and build without too much effort.