如何在SQL Server调优顾问程序中记录SqlCommand查询以进行分析

时间:2014-09-15 16:10:40

标签: c# sql sql-server trace sqlcommand

我有一个DB连接类,它执行我的所有数据库连接

我想捕获查询并在SQL Server调优顾问程序上运行它们以找出丢失的索引等

SQL Server Profiler调整需要永远处理。所以我想撰写自己的文件进行分析

问题是我不知道如何捕获查询以正确分析

这是我的SqlCommand函数

SQL server 2014,c#.net 4.5

我的问题是我应该记录哪些参数,值等以完美地分析它们

        public static DataTable cmd_SelectQuery(string srCommandText, List<string> lstParameterNames, List<object> lstParameters)
    {
        DataTable dsCmdPara = new DataTable();
        try
        {
            using (SqlConnection connection = new SqlConnection(DbConnection.srConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(srCommandText, connection))
                {
                    cmd.CommandTimeout = PublicSettings.irCommandTimeOutSettings_Second;
                    cmd.CommandType = CommandType.Text;

                    object objParameter;

                    for (int i = 0; i < lstParameterNames.Count; i++)
                    {
                        objParameter = lstParameters[i];
                        if (objParameter == null)
                            objParameter = "n";

                        cmd.Parameters.AddWithValue(lstParameterNames[i], lstParameters[i].ToString());
                    }
                    using (SqlDataAdapter sqlDa = new SqlDataAdapter(cmd))
                    {
                        sqlDa.Fill(dsCmdPara);
                        return dsCmdPara;
                    }
                }
            }
        }
        catch (Exception E)
        {
            Interlocked.Increment(ref GlobalStats.long_GlobalSQLErrorCount);

            StringBuilder srParameters = new StringBuilder();
            if (PublicSettings.blLogSqlParametersOnErrors == true)
            {
                srParameters.AppendLine("");
                srParameters.AppendLine("error at : cmd_UpdateDeleteQuery");
                foreach (var vrItem in lstParameters)
                {
                    srParameters.AppendLine("per parameter: " + vrItem + " \r\n\r\n");
                }
            }

            if (PublicSettings.blLogSqlErrors)
                ErrorLogger.LogSQL_Error("Error at: cmd_SelectQuery " + srCommandText + " " + E.Message.ToString() + " \r\n" + srParameters.ToString());
        }
        return dsCmdPara;
    }

    public static bool cmd_UpdateDeleteQuery(string srCommandText, List<string> lstParameterNames, List<object> lstParameters)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(DbConnection.srConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(srCommandText, connection))
                {
                    cmd.CommandTimeout = PublicSettings.irCommandTimeOutSettings_Second;
                    cmd.CommandType = CommandType.Text;

                    object objParameter;

                    for (int i = 0; i < lstParameterNames.Count; i++)
                    {
                        objParameter = lstParameters[i];
                        if (objParameter == null)
                            objParameter = "n";

                        cmd.Parameters.AddWithValue(lstParameterNames[i], objParameter);
                    }
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    return true;
                }
            }
        }
        catch (Exception E)
        {
            Interlocked.Increment(ref GlobalStats.long_GlobalSQLErrorCount);

            StringBuilder srParameters = new StringBuilder();
            if (PublicSettings.blLogSqlParametersOnErrors == true)
            {
                srParameters.AppendLine("");
                srParameters.AppendLine("error at : cmd_UpdateDeleteQuery");
                foreach (var vrItem in lstParameters)
                {
                    srParameters.AppendLine("per parameter: " + vrItem + " \r\n\r\n");
                }
            }

            if (PublicSettings.blLogSqlErrors)
                ErrorLogger.LogSQL_Error("Error at: cmd_UpdateDeleteQuery " + srCommandText + " " + E.Message.ToString() + srParameters.ToString());
        }
        return false;
    }

0 个答案:

没有答案