You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bantflags/BantFlags/Data/Query.cs

61 lines
1.4 KiB

5 years ago
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Threading.Tasks;
namespace BantFlags.Data
{
/// <summary>
/// Succinct methods for creating and executing database queries
/// </summary>
public class Query : IDisposable
{
private MySqlCommand Command { get; }
public Query(MySqlCommand cmd)
{
Command = cmd;
}
public async Task<DataTable> ExecuteTableAsync()
{
using (var reader = await Command.ExecuteReaderAsync())
{
DataTable table = new DataTable();
table.Load(reader);
return table;
}
}
public async Task ExecuteNonQueryAsync(bool reuse = false)
{
await Command.ExecuteNonQueryAsync();
if (!reuse)
{
Dispose();
}
}
public Query SetParam(string parameter, object value)
{
// When we reuse a query, we write over the parameter.
if (Command.Parameters.Contains(parameter))
{
Command.Parameters[parameter].Value = value;
}
else
{
Command.Parameters.AddWithValue(parameter, value);
}
return this;
}
public void Dispose()
{
Command.Dispose();
}
}
}