Created
February 2, 2020 00:31
-
-
Save Axemasta/649c92df900a5e2468a5aa8b59916200 to your computer and use it in GitHub Desktop.
SqlHelper Class
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
| 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