This method allows for the execution of parameterized commands within a DAL class, and I tried to make it as reusable as possible. It works well but it feels a bit verbose to me. Is there a way to simplify this or is it good as is?
/// <summary>
/// Retrieves records from the specified data source.
/// </summary>
/// <param name="query">The command to be executed.</param>
/// <param name="dataSrc">The data source to use for the command.</param>
/// <param name="parameters">Optional parameters for a parameterized command.</param>
/// <returns>A System.Data.DataTable containing the returned records.</returns>
public static DataTable GetRecords(string query, DelConnection dataSrc, params SqlParameter[] parameters)
{
if (query == "") throw new ArgumentException("No command provided.", "query");
string conString = Dal.GetConnectionString(dataSrc);
using (SqlConnection con = new SqlConnection(conString))
using (SqlCommand cmd = new SqlCommand(query, con))
{
// Add parameters to command
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
// Fill data table
DataTable data = new DataTable();
using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
{
adap.Fill(data);
}
return data;
}
} //// End GetRecords()
null
when the result is empty? It just makes the code that works with the result more complicated (it always has to check fornull
), more error prone (what if your forget to check fornull
?) and doesn't actually give you anything. – svick Apr 25 at 22:14DelConnection
? I can't find it on Google. Is it an enum likeDevelopment
,Test
,Production
etc? – abuzittin gillifirca Apr 26 at 13:25