Which SQL is sent from the SqlCommand object

I have a SqlCommand object in my asp.net page in C #. SQL and passed parameters work most of the time. I have one case that does not work, I get the following error:

String or binary data will be truncated. Application completed.

I understand the error, but all the columns in the database should be long enough to keep everything sent.

My questions,

Is there any way to see which actual SQL being sent to the database from the SqlCommand object? I want to be able to send emails to SQL when an error occurs.

Thanks Justin

+2
source share
4 answers

Profiler SQL Server , . , SQL , .

+7

- Enterprise Manager , .

public static string ToReadableString(this IDbCommand command)
{
    StringBuilder builder = new StringBuilder();
    if (command.CommandType == CommandType.StoredProcedure)
        builder.AppendLine("Stored procedure: " + command.CommandText);
    else
        builder.AppendLine("Sql command: " + command.CommandText);
    if (command.Parameters.Count > 0)
        builder.AppendLine("With the following parameters.");
    foreach (IDataParameter param in command.Parameters)
    {
        builder.AppendFormat(
            "     Paramater {0}: {1}",
            param.ParameterName,
            (param.Value == null ? 
            "NULL" : param.Value.ToString())).AppendLine();
    }
    return builder.ToString();
}
+8

, - - TSQL - ... :)

OK .., " " SSMS. SP, "text" ..

public static String ParameterValueForSQL(this SqlParameter sp)
  {
   String retval = "";

   switch (sp.SqlDbType)
   {
    case SqlDbType.Char:
    case SqlDbType.NChar:
    case SqlDbType.NText:
    case SqlDbType.NVarChar:
    case SqlDbType.Text:
    case SqlDbType.Time:
    case SqlDbType.VarChar:
    case SqlDbType.Xml:
    case SqlDbType.Date:
    case SqlDbType.DateTime:
    case SqlDbType.DateTime2:
    case SqlDbType.DateTimeOffset:
     retval = "'" + sp.Value.ToString().Replace("'", "''") + "'";
     break;

    case SqlDbType.Bit:
     retval = (sp.Value.ToBooleanOrDefault(false)) ? "1" : "0";
     break;

    default:
     retval = sp.Value.ToString().Replace("'", "''");
     break;
   }

   return retval;
  }

  public static String CommandAsSql(this SqlCommand sc)
  {
   StringBuilder sql = new StringBuilder();
   Boolean FirstParam = true;

   sql.AppendLine("use " + sc.Connection.Database + ";");
   switch (sc.CommandType)
   {
    case CommandType.StoredProcedure:
     sql.AppendLine("declare @return_value int;");

     foreach (SqlParameter sp in sc.Parameters)
     {
      if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
      {
       sql.Append("declare " + sp.ParameterName + "\t" + sp.SqlDbType.ToString() + "\t= ");

       sql.AppendLine(((sp.Direction == ParameterDirection.Output) ? "null" : sp.ParameterValueForSQL()) + ";");

      }
     }

     sql.AppendLine("exec [" + sc.CommandText + "]");

     foreach (SqlParameter sp in sc.Parameters)
     {
      if (sp.Direction != ParameterDirection.ReturnValue)
      {
       sql.Append((FirstParam) ? "\t" : "\t, ");

       if (FirstParam) FirstParam = false;

       if (sp.Direction == ParameterDirection.Input)
        sql.AppendLine(sp.ParameterName + " = " + sp.ParameterValueForSQL());
       else

        sql.AppendLine(sp.ParameterName + " = " + sp.ParameterName + " output");
      }
     }
     sql.AppendLine(";");

     sql.AppendLine("select 'Return Value' = convert(varchar, @return_value);");

     foreach (SqlParameter sp in sc.Parameters)
     {
      if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
      {
       sql.AppendLine("select '" + sp.ParameterName + "' = convert(varchar, " + sp.ParameterName + ");");
      }
     }
     break;
    case CommandType.Text:
     sql.AppendLine(sc.CommandText);
     break;
   }

   return sql.ToString();
  }

...

use dbMyDatabase;
declare @return_value int;
declare @OutTotalRows BigInt = null;
exec [spMyStoredProc]
 @InEmployeeID = 1000686
 , @InPageSize = 20
 , @InPage = 1
 , @OutTotalRows = @OutTotalRows output
;
select 'Return Value' = convert(varchar, @return_value);
select '@OutTotalRows' = convert(varchar, @OutTotalRows);
+4

Check this question, it should provide what you are looking for.

Get generated SQL statement from SqlCommand?

+3
source

Source: https://habr.com/ru/post/1769766/


All Articles