Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. MSCSqlException msx = new MSCSqlException(sqlQuery, sqlParams, sqlException);
  2. throw new Exception(ex.Message);
  3.  
  4. MSCSqlException msx = new MSCSqlException(sqlQuery, sqlParams, sqlException);
  5. throw new Exception(ex.Message, ex);
  6.  
  7. throw new MSCSqlException(sqlQuery, sqlParams, sqlException);
  8.  
  9. [Serializable]
  10. public class MSCSqlException : Exception
  11. {
  12. public string SqlCommand { get; private set; }
  13. public object[] SqlParameters { get; private set; }
  14.  
  15. public MSCSqlException()
  16. {
  17. }
  18.  
  19. public MSCSqlException(string message)
  20. : base(message)
  21. {
  22. }
  23.  
  24. public MSCSqlException(string message, Exception inner) : base(message, inner)
  25. {
  26. }
  27.  
  28. public MSCSqlException(string command, object[] parameters, SqlException sx) : base(CreateUsefulMessage(sx, command, parameters), sx)
  29. {
  30. SqlCommand = command;
  31. SqlParameters = parameters;
  32. }
  33.  
  34. protected MSCSqlException(SerializationInfo info, StreamingContext context) : base(info, context)
  35. {
  36. SqlCommand = info.GetString("SqlCommand");
  37. }
  38.  
  39. [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
  40. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  41. {
  42. if (info == null)
  43. {
  44. throw new ArgumentNullException("info");
  45. }
  46.  
  47. info.AddValue("SqlCommand", SqlCommand);
  48. base.GetObjectData(info, context);
  49. }
  50.  
  51. public static string CreateUsefulMessage(SqlException sx, string sqlCommand, object[] sqlParameters)
  52. {
  53. string message = sx.Message + Environment.NewLine;
  54.  
  55. if(sqlParameters != null && sqlParameters.Count() > 0)
  56. {
  57. message = message + "Parameters:" + Environment.NewLine;
  58. foreach(object sp in sqlParameters)
  59. {
  60. message = message + "t" + sp.ToString() + Environment.NewLine;
  61. }
  62. }
  63.  
  64. message = message + "SQL Statement:" + Environment.NewLine;
  65. message = message + sqlCommand;
  66.  
  67. return message;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement