Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataBase : Page
- {
- protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- protected static string ConnectionString;
- public DataBase()
- {
- ConnectionString = GetConnectionString();
- }
- public static String GetConnectionString()
- {
- return ConfigurationManager.ConnectionStrings["abc"].ConnectionString;
- }
- public static T GetValue<T>(string query)
- where T : IComparable, IConvertible, IEquatable<T>
- {
- Object value = GetValue(query);
- if (Convert.IsDBNull(value))
- return GetDefaultValue<T>();
- return (T)Convert.ChangeType(value, typeof(T));
- }
- public static T GetDefaultValue<T>()
- where T : IComparable, IConvertible, IEquatable<T>
- {
- if (typeof(T) == typeof(String))
- return (T)(object)String.Empty;
- return default(T);
- }
- private static Object GetValue(string query)
- {
- try
- {
- using (SqlConnection connection = new SqlConnection(ConnectionString))
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- connection.Open();
- return command.ExecuteScalar();
- }
- }
- catch (Exception e)
- {
- LogQueryError(query, e);
- return DBNull.Value;
- }
- }
- protected static void LogQueryError(string query, Exception e)
- {
- log.Error(string.Format("Error while executing Query ({0}): {1}", query, e.Message));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement