Can I get a T-SQL query created from LinqDataSource?

I am using LinqDataSource to populate the grid. But now I need the SQL query that LinqDataSource generates to pass throught methods (no, I can’t change the methods that don’t need the SQL query).

Is there a way to get the generated SQL query from the created and configured LinqDataSource?

+3
source share
4 answers

Hope this helps.

using the function below, will return SqlQueryText you can rebuild the query from this object.

  • to get sql text that you can use use the .Text property
  • , .Params

        public static SqlQueryText GetFullQueryInfo(DataContext dataContext, IQueryable query)
        {
            DbCommand dbCommand = dataContext.GetCommand(query);
    
            var result = new SqlQueryText();
    
            result.Text = dbCommand.CommandText;
            int nParams = dbCommand.Parameters.Count;
            result.Params = new ParameterText[nParams];
            for (int j = 0; j < nParams; j++)
            {
                var param = new ParameterText();
                DbParameter pInfo = dbCommand.Parameters[j];
                param.Name = pInfo.ParameterName;
                param.SqlType = pInfo.DbType.ToString();
                object paramValue = pInfo.Value;
                if (paramValue == null)
                {
                    param.Value = null;
                }
                else
                {
                    param.Value = pInfo.Value.ToString();
                }
                result.Params[j] = param;
            }
            return result;
        }
    

    var results = db.Medias.Where(somepredicatehere); ClassThatHasThisMethod.GetFullQueryInfo(yourdatacontexthere, results);

EDIT:

, SqlQueryText

public struct SqlQueryText
{
    public ParameterText[] Params;
    public string Text;
}

public struct ParameterText
{
    public string Name;
    public string SqlType;
    public string Value;
}
+3

SQL Profiler .

+2

LinqPad , . , , .

+1

Sql Linq to Sql .

, , Sql , linq Sql, Sql?

I found the Linq To Sql Debug visualizer on the Scottgu blog .

0
source

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


All Articles