我可以从LinqDataSource生成T-SQL查询吗?

时间:2022-10-06 21:10:37

I´m using the LinqDataSource to populate a grid. But now I need the SQL query that the LinqDataSource generates, to pass around throught methods (no, I can't modify the methods to not need a SQL query).

我使用LinqDataSource´填充一个网格。但是现在我需要LinqDataSource生成的SQL查询,以便通过各种方法进行传递(不,我不能修改这些方法以不需要SQL查询)。

Is there a way to obtain the generated SQL query from a instantiated and configured LinqDataSource?

是否有方法从实例化和配置的LinqDataSource获取生成的SQL查询?

4 个解决方案

#1


3  

Hope this helps.

希望这个有帮助。

using the function below will return a SqlQueryText you can rebuild the query from that object.

使用下面的函数将返回一个SqlQueryText,您可以从该对象重新构建查询。

  • to get the sql text you can use use the .Text Property
  • 要获取sql文本,可以使用.Text属性
  • to get the passed parameters you can use the .Params 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;
        }
    

    here is an example

    这是一个例子

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

    var =结果db.Medias.Where(somepredicatehere);ClassThatHasThisMethod。GetFullQueryInfo(yourdatacontexthere,结果);

EDIT:

编辑:

Sorry forgot to include the SqlQueryText data structures

抱歉忘记包含SqlQueryText数据结构

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

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

#2


2  

You can run SQL Profiler while running your application and that should give it to you.

您可以在运行应用程序时运行SQL分析器,这应该是您的。

#3


1  

Take a look at LinqPad for debugging and to understand how it works. But if you want it at run-time, I think you're out of luck.

查看LinqPad进行调试并了解它是如何工作的。但如果你想在运行时使用它,我认为你运气不佳。

#4


0  

The Sql will only be generated by the Linq to Sql infrastructure at runtime.

Sql将只在运行时由Linq到Sql基础结构生成。

I think there are some tools to see generated Sql in the debugger, but if you don't plan to use linq to generate your Sql dynamicaly, shouldn't you probably look for a simple Sql designer ?

我认为有一些工具可以在调试器中看到生成的Sql,但是如果您不打算使用linq来生成Sql dynamicaly,难道您不应该寻找一个简单的Sql设计器吗?

I Found a Linq To Sql Debug visualizer on Scottgu's blog.

我在Scottgu的博客上找到了一个Linq To Sql Debug可视化工具。

#1


3  

Hope this helps.

希望这个有帮助。

using the function below will return a SqlQueryText you can rebuild the query from that object.

使用下面的函数将返回一个SqlQueryText,您可以从该对象重新构建查询。

  • to get the sql text you can use use the .Text Property
  • 要获取sql文本,可以使用.Text属性
  • to get the passed parameters you can use the .Params 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;
        }
    

    here is an example

    这是一个例子

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

    var =结果db.Medias.Where(somepredicatehere);ClassThatHasThisMethod。GetFullQueryInfo(yourdatacontexthere,结果);

EDIT:

编辑:

Sorry forgot to include the SqlQueryText data structures

抱歉忘记包含SqlQueryText数据结构

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

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

#2


2  

You can run SQL Profiler while running your application and that should give it to you.

您可以在运行应用程序时运行SQL分析器,这应该是您的。

#3


1  

Take a look at LinqPad for debugging and to understand how it works. But if you want it at run-time, I think you're out of luck.

查看LinqPad进行调试并了解它是如何工作的。但如果你想在运行时使用它,我认为你运气不佳。

#4


0  

The Sql will only be generated by the Linq to Sql infrastructure at runtime.

Sql将只在运行时由Linq到Sql基础结构生成。

I think there are some tools to see generated Sql in the debugger, but if you don't plan to use linq to generate your Sql dynamicaly, shouldn't you probably look for a simple Sql designer ?

我认为有一些工具可以在调试器中看到生成的Sql,但是如果您不打算使用linq来生成Sql dynamicaly,难道您不应该寻找一个简单的Sql设计器吗?

I Found a Linq To Sql Debug visualizer on Scottgu's blog.

我在Scottgu的博客上找到了一个Linq To Sql Debug可视化工具。