从 SqlCommand 对象发送什么 SQL

发布于 2024-08-28 00:04:57 字数 263 浏览 6 评论 0原文

我的基于 c# 的 asp.net 页面上有一个 SqlCommand 对象。 SQL 和传递的参数大部分时间都在工作。我有一种情况不起作用,出现以下错误:

字符串或二进制数据将被截断。该声明已终止。

我理解该错误,但数据库中的所有列都应该足够长以容纳正在发送的所有内容。

我的问题是,

有没有办法查看从 SqlCommand 对象发送到数据库的实际 SQL 是什么?我希望能够在发生错误时通过电子邮件发送 SQL。

谢谢, 贾斯汀

I have a SqlCommand object on my c# based asp.net page. The SQL and the passed parameters are working the majority of the time. I have one case that is not working, I get the following error:

String or binary data would be truncated. The statement has been terminated.

I understand the error and but all the columns in the database should be long enough to hold everything being sent.

My questions,

Is there a way to see what the actual SQL being sent to the database is from SqlCommand object? I would like to be able to email the SQL when an error occurs.

Thanks,
Justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中的蝴蝶 2024-09-04 00:04:57

虽然您无法将其插入企业管理器之类的东西来运行它,但它可以用于日志记录。

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();
}

While you will not be able to plug is into something like Enterprise Manager to run it works for logging.

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();
}
甜心 2024-09-04 00:04:57

您需要使用 SQL Server Profiler 来观察来自应用程序的内容。我相信它可以向您显示您需要查看的 SQL 和参数。

You need to use the SQL Server Profiler to watch what comes from the application. I believe it can show you the SQL and the parameters, which you will need to see.

芯好空 2024-09-04 00:04:57

虽然并不完美,但这里有一些我为 TSQL 开发的东西 - 可以很容易地调整为其他风格...如果没有别的,它将为您自己的改进提供一个起点:)

这在数据类型和输出参数方面做得很好等类似于在SSMS中使用“执行存储过程”。我们主要使用 SP,因此“文本”命令不考虑参数等,

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);

Whilst not perfect, here's something I knocked up something for TSQL - could be easily tweaked for other flavours... If nothing else it will give you a start point for your own improvements :)

This does an OK job on data types and output parameters etc similar to using "execute stored procedure" in SSMS. We mostly used SPs so the "text" command doesn't account for parameters etc

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();
  }

this generates output along these lines...

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);
浅唱々樱花落 2024-09-04 00:04:57

查看这个问题,它应该提供您正在寻找的内容。

从 SqlCommand 对象获取生成的 SQL 语句?

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

Get the generated SQL statement from a SqlCommand object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文