SqlCommandExt.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Text;
  5. namespace Crestyl.DMS.Console.Migration.Commands.Migration.Assets
  6. {
  7. public static class SqlCommandExt
  8. {
  9. private static string ParameterValueForSQL(this SqlParameter sp)
  10. {
  11. var result = "";
  12. if (sp.Value == null || sp.Value == DBNull.Value)
  13. return "NULL";
  14. switch (sp.SqlDbType)
  15. {
  16. case SqlDbType.UniqueIdentifier:
  17. result = "'" + ((Guid)sp.Value).ToString("B") + "'";
  18. break;
  19. case SqlDbType.Char:
  20. case SqlDbType.NChar:
  21. case SqlDbType.NText:
  22. case SqlDbType.NVarChar:
  23. case SqlDbType.Text:
  24. case SqlDbType.Time:
  25. case SqlDbType.VarChar:
  26. case SqlDbType.Xml:
  27. result = "'" + sp.Value.ToString().Replace("'", "''") + "'";
  28. break;
  29. case SqlDbType.Date:
  30. case SqlDbType.DateTime:
  31. case SqlDbType.DateTime2:
  32. case SqlDbType.DateTimeOffset:
  33. result = "'" + ((DateTime)sp.Value).ToString("s").Replace("'", "''") + "'";
  34. break;
  35. case SqlDbType.Bit:
  36. result = ((int)sp.Value == 1) ? "1" : "0";
  37. break;
  38. default:
  39. result = sp.Value.ToString().Replace("'", "''");
  40. break;
  41. }
  42. return result;
  43. }
  44. public static string CommandAsSql(this SqlCommand sc)
  45. {
  46. StringBuilder sql = new StringBuilder();
  47. var isFirst = true;
  48. sql.AppendLine("use " + sc.Connection.Database + ";");
  49. switch (sc.CommandType)
  50. {
  51. case CommandType.StoredProcedure:
  52. sql.AppendLine("declare @return_value int;");
  53. foreach (SqlParameter sp in sc.Parameters)
  54. {
  55. if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
  56. {
  57. sql.Append("declare " + sp.ParameterName + "\t" + sp.SqlDbType.ToString() + "\t= ");
  58. sql.AppendLine(((sp.Direction == ParameterDirection.Output) ? "null" : sp.ParameterValueForSQL()) + ";");
  59. }
  60. }
  61. sql.AppendLine("exec [" + sc.CommandText + "]");
  62. foreach (SqlParameter sp in sc.Parameters)
  63. {
  64. if (sp.Direction != ParameterDirection.ReturnValue)
  65. {
  66. sql.Append((isFirst) ? "\t" : "\t, ");
  67. if (isFirst) isFirst = false;
  68. if (sp.Direction == ParameterDirection.Input)
  69. sql.AppendLine(sp.ParameterName + " = " + sp.ParameterValueForSQL());
  70. else
  71. sql.AppendLine(sp.ParameterName + " = " + sp.ParameterName + " output");
  72. }
  73. }
  74. sql.AppendLine(";");
  75. sql.AppendLine("select 'Return Value' = convert(varchar, @return_value);");
  76. foreach (SqlParameter sp in sc.Parameters)
  77. {
  78. if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
  79. {
  80. sql.AppendLine("select '" + sp.ParameterName + "' = convert(varchar, " + sp.ParameterName + ");");
  81. }
  82. }
  83. break;
  84. case CommandType.Text:
  85. foreach (SqlParameter sp in sc.Parameters)
  86. {
  87. if (sp.Direction == ParameterDirection.Input)
  88. {
  89. sql.Append("declare " + sp.ParameterName + "\t" + sp.SqlDbType.ToString() + "\t= ");
  90. sql.AppendLine(((sp.Direction == ParameterDirection.Output) ? "null" : sp.ParameterValueForSQL()) + ";");
  91. }
  92. }
  93. sql.AppendLine(sc.CommandText);
  94. break;
  95. }
  96. return sql.ToString();
  97. }
  98. }
  99. }