【问题标题】:SQL query generated by LINQ TO SQL statementLINQ TO SQL 语句生成的 SQL 查询
【发布时间】:2009-10-16 18:19:12
【问题描述】:

我如何知道我的 Linq to sql 查询生成的 SQL 语句?

【问题讨论】:

    标签: linq-to-sql


    【解决方案1】:

    您可以使用 toString() 语句查看 SQL 语句。

    var customers = from cust in Customers
            select cust;
    
    Console.WriteLine(customers.ToString());
    

    或者你可以这样做。

    DataContext context = new DataContext(...);
    StringWriter writer = new StringWriter();
    context.Log = writer;
    
    var customers = from cust in Customers
            select cust;
    
    Console.WriteLine(writer.ToString());
    

    【讨论】:

    • 哇,谁知道呢。我已经使用 LINQ 几年了,从来不知道 Query.ToString 会返回 SQL 命令。
    【解决方案2】:

    使用LINQ to SQL Debugger Visualizer

    或者,您可以将dataContext.Log 属性设置为Console.Out 或其他内容,SQL 语句以及实际参数值将被写入该流。

    【讨论】:

      【解决方案3】:

      有检查查询的工具http://www.linqpad.net/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多