【问题标题】:Possible to get the stored procedure call for debugging?可以获取存储过程调用以进行调试吗?
【发布时间】:2011-07-05 17:27:28
【问题描述】:

在我的 ASP.NET 网站上使用 Visual Studio 2010 时,我们有用于存储过程调用的代码:

SqlDataAccess sqlDataAccess = new SqlDataAccess();

SqlParameter[] parameters =
{
    new SqlParameter("@R", rptType.Replace("'", "''")),
    new SqlParameter("@M", hybrDct["mod"].ToString().Replace("'", "''")),
    new SqlParameter("@C", hybrDct["CG"].ToString().Replace("'", "''")),
    new SqlParameter("@Ts$", hybrDct["TFields"].ToString().Replace("'", "''")),
};

sqlDataAccess.ProcName = "MyStoredProc";
sqlDataAccess.Parameters = parameters;

是否可以将执行打印出来以用于调试目的,而不是找出每个单独的 SqlParameter 并单独输入?

谢谢。

【问题讨论】:

    标签: c# .net stored-procedures ado.net


    【解决方案1】:

    我使用类似以下的东西 - 一切都通过这个并被记录 - 你明白了。

    /// <summary>
    /// Executes a stored procedure with no return.
    /// </summary>
    /// <returns>The number of records affected by stored proc.</returns>
    public static int ExecuteStoredProc(string storedProcName, params SqlParameter[] parameters)
    {        
        StringBuilder callDefinition = new StringBuilder();
        callDefinition.Append(string.Format("ExecuteStoredProc: {0} (", storedProcName));
        for (int i = 0; i < parameters.Count(); i++)
        {
            callDefinition.Append(string.Format("{0}={1}", parameters[i].ParameterName, parameters[i].Value));
            if (i < parameters.Count - 1)
            {
                callDefinition.Append(",");
            }
        }
        callDefinition.Append(")";
        log.Debug(callDefinition.ToString());
        using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionProfile.ConnectionName))
        {
            using (SqlCommand command = new SqlCommand(storedProcName, ctx.Connection))
            {
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandTimeout = 1000;
                foreach (SqlParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                    //log your param here
                }
    
                return command.ExecuteNonQuery();
            }
        }
    }
    
    /// <summary>
    /// Executes a query and returns a dataset
    /// </summary>
    public static DataSet ExecuteQueryReturnDataSet(string query, params SqlParameter[] parameters)
    {
        try
        {
            //implement the parameter logging here as in the above code sample as well
    
            log.Debug("Executing ExecuteQueryReturnDataSet() calling query " + query);
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionProfile.ConnectionName))
            {
                using (SqlCommand command = new SqlCommand(query, ctx.Connection))
                {
                    command.CommandType = System.Data.CommandType.Text;
                    command.CommandTimeout = 1000;
                    foreach (SqlParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                    {
                        DataSet dataSet = new DataSet();
                        adapter.Fill(dataSet);
                        return dataSet;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            throw;
        }
    }
    

    【讨论】:

    • 谢谢 - 前提是您不想在那里为任何特定值进行日志记录、dbnull 检查等。
    【解决方案2】:

    我通常运行 SQL Server Profiler(假设这是您的数据库)并且可以从日志中复制完整的查询。

    【讨论】:

      【解决方案3】:

      不幸的是 SqlClient(我假设您正在使用它来实现 SqlDataAccess,我还假设它是您自己的数据访问层)不直接公开发送到 SQL Server 的命令。我通常在我的数据访问层中执行的操作是以可以在查询窗口中执行的格式记录所有命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 2020-04-03
        • 1970-01-01
        • 2018-09-22
        • 2018-02-13
        • 2017-04-07
        相关资源
        最近更新 更多