Last active
August 29, 2015 14:15
-
-
Save rostreim/184da11106cb762eb61a to your computer and use it in GitHub Desktop.
Database.cs Table method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns the result Table of the specified SQL. | |
/// </summary> | |
/// <param name="sql">The SQL.</param> | |
/// <param name="parameters">The parameters.</param> | |
/// <param name="commandType">Type of the command.</param> | |
/// <returns></returns> | |
internal DataTable Table(string sql, dynamic parameters = null, CommandType? commandType = null) { | |
DataTable table = new DataTable(); | |
using (SqlCommand command = new SqlCommand(sql, CreateConnection())) { | |
if (commandType != null) | |
command.CommandType = commandType.Value; | |
IDictionary<string, string> parameterDictionary = parameters as IDictionary<string, string>; | |
if (parameterDictionary != null) | |
foreach (var parameter in parameterDictionary.Keys) | |
command.Parameters.AddWithValue("@" + parameter, parameterDictionary[parameter]); | |
else | |
if (parameters != null) | |
foreach (var property in parameters.GetType().GetProperties()) | |
command.Parameters.AddWithValue("@" + property.Name, property.GetGetMethod().Invoke(parameters, null)); | |
using (SqlDataAdapter adapter = new SqlDataAdapter(command)) | |
adapter.Fill(table); | |
return table; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment