Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created February 2, 2020 00:31
Show Gist options
  • Select an option

  • Save Axemasta/649c92df900a5e2468a5aa8b59916200 to your computer and use it in GitHub Desktop.

Select an option

Save Axemasta/649c92df900a5e2468a5aa8b59916200 to your computer and use it in GitHub Desktop.
SqlHelper Class
public static class SqlHelper
{
#region Methods
#region - Public Methods
/// <summary>
/// Replace * In SQL Statement With Object Properties
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="tableAbbreviation"></param>
/// <returns></returns>
public static string ReplaceStar<T>(string sql, string tableAbbreviation = null) where T : Axemasta.MusicLibrary.App.Models.Artist
{
List<string> propertyNames = new List<string>();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
string wrapper = string.Empty;
if (tableAbbreviation != null)
{
wrapper = tableAbbreviation + ".";
}
foreach (var property in properties)
{
propertyNames.Add(wrapper + "[" + property.Name + "]");
}
string fields = string.Join(", ", propertyNames);
return sql.Replace("*", fields);
}
/// <summary>
/// Replace * In SQL Statement With Given Fields
/// </summary>
/// <param name="sql"></param>
/// <param name="tableAbbreviation"></param>
/// <param name="properties"></param>
/// <returns></returns>
public static string ReplaceStar(string sql, string tableAbbreviation = null, params string[] properties)
{
List<string> propertyNames = new List<string>();
string wrapper = string.Empty;
if (tableAbbreviation != null)
{
wrapper = tableAbbreviation + ".";
}
foreach (var property in properties)
{
propertyNames.Add(wrapper + "[" + property + "]");
}
string fields = string.Join(", ", propertyNames);
return sql.Replace("*", fields);
}
#endregion - Public Methods
#region - Helper Methods
#endregion - Helper Methods
#endregion Methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment