如何从 LINQ DataContext.SubmitChanges() 获取 TSQL 查询

发布于 2024-07-15 08:28:29 字数 150 浏览 10 评论 0 原文

我正在使用 Linq to SQL。 我有一个 DataContext,我正在对其进行 .SubmitChanges()'ing。 插入身份字段时出错,我想查看它用于插入此身份字段的查询。

我在快速观看中没有看到查询本身; 我可以从调试器中的哪里找到它?

I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing. There is an error inserting the identity field, and I'd like to see the query it's using to insert this identity field.

I don't see the query itself within the quickwatch; where can I find it from within the debugger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

百善笑为先 2024-07-22 08:28:29

许多人一直在编写自己的“DebugWriter”并像这样附加它:

// Add this class somewhere in your project...
class DebugTextWriter : System.IO.TextWriter {
   public override void Write(char[] buffer, int index, int count) {
       System.Diagnostics.Debug.Write(new String(buffer, index, count));
   }

   public override void Write(string value) {
       System.Diagnostics.Debug.Write(value);
   }

   public override Encoding Encoding {
       get { return System.Text.Encoding.Default; }
   }
}

// Then attach it to the Log property of your DataContext...
myDataContext.Log = new DebugTextWriter()

这会将 Linq-to-Sql 正在执行的所有操作输出到 Visual Studio 的调试窗口中。

Lots of people have been writing their own "DebugWriter" and attaching it like so:

// Add this class somewhere in your project...
class DebugTextWriter : System.IO.TextWriter {
   public override void Write(char[] buffer, int index, int count) {
       System.Diagnostics.Debug.Write(new String(buffer, index, count));
   }

   public override void Write(string value) {
       System.Diagnostics.Debug.Write(value);
   }

   public override Encoding Encoding {
       get { return System.Text.Encoding.Default; }
   }
}

// Then attach it to the Log property of your DataContext...
myDataContext.Log = new DebugTextWriter()

This will output everything that Linq-to-Sql is doing into Visual Studio's debug window.

夕嗳→ 2024-07-22 08:28:29

进一步波特曼的回答 ,如果您是控制台应用程序,则很简单:

myDataContext.Log = Console.Out;

或者您可以使用 Linq2SQL Profiler 之类的东西,这是一个相当出色的工具,实际上是完成这项工作的正确工具:

Linq to SQL Profiler - Linq 的实时可视化调试器到 SQL

Further to Portman's answer, if you're a console application it's as simple as:

myDataContext.Log = Console.Out;

Or you could use something like Linq2SQL Profiler which is a rather excellent tool and in fact the right tool for the job:

Linq to SQL Profiler - Real-time visual debugger for Linq to SQL

末蓝 2024-07-22 08:28:29

实际上你的问题有一个非常简单的答案

只需将其粘贴到你的手表窗口中

((System.Data.Objects.ObjectQuery)myLinqQueryVar).ToTraceString()

There is actually a very simple answer to your question

Just paste this in your watch window

((System.Data.Objects.ObjectQuery)myLinqQueryVar).ToTraceString()
朦胧时间 2024-07-22 08:28:29

运行 SQL Profiler(如果有)。 它将显示数据库的所有流量,包括 SQL 命令文本。

Run SQL Profiler if you have it. It'll show all traffic to your database, including SQL command text.

我最亲爱的 2024-07-22 08:28:29
FooDataContext dc = new FooDataContext();

StringBuilder sb = new StringBuilder();
dc.Log = new StringWriter(sb);

var result=from r in dc.Tables select d;

.....
string query=sb.ToString();
FooDataContext dc = new FooDataContext();

StringBuilder sb = new StringBuilder();
dc.Log = new StringWriter(sb);

var result=from r in dc.Tables select d;

.....
string query=sb.ToString();
饮惑 2024-07-22 08:28:29

我同意 Linq to SQL Profiler 是完成这项工作的正确工具。 但如果您不想花钱或者只需要做一些简单的事情,我喜欢 DebugTextWriter 方法。

读完这个问题后,我开始寻找更强大的东西。 事实证明,Damien Guard 还 写了一篇非常好的文章,介绍如何构建不同的编写器来处理不同的事情,例如输出到内存、调试、文件、多个目标,甚至使用简单的委托。

我最终使用了他的一些想法并编写了一个可以处理多个委托的 ActionTextWriter,我想我应该在这里分享它:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Writers
{
    public class ActionTextWriter : TextWriter
    {
        protected readonly List<Action<string>> Actions = new List<Action<string>>();

        public ActionTextWriter(Action<string> action)
        {
            Actions.Add(action);
        }

        public ActionTextWriter(IEnumerable<Action<string>> actions)
        {
            Actions.AddRange(actions);
        }

        public ActionTextWriter(params Action<string>[] actions)
        {
            Actions.AddRange(actions);
        }

        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Write(new string(buffer, index, count));
        }

        public override void Write(char value)
        {
            Write(value.ToString());
        }

        public override void Write(string value)
        {
            if (value == null)
            {
                return;
            }

            foreach (var action in Actions)
            {
                action.Invoke(value);
            }
        }
    }
}

您可以添加任意数量的操作。 此示例通过 Debug.Write 写入 Visual Studio 中的日志文件和控制台:

// Create data context
var fooDc = new FooDataContext();

// Create writer for log file.
var sw = new StreamWriter(@"C:\DataContext.log") {AutoFlush = true};

// Create write actions.
Action<string> writeToDebug = s => Debug.Write(s);
Action<string> writeToLog = s => sw.Write(s);

// Wire up log writers.
fooDc.Log = new ActionTextWriter(writeToDebug, writeToLog);

当然,如果您想制作更简单的文件以便即兴使用,您可以随时扩展 ActionTextWriter...编写通用方法和重用,对吗?

using System.Diagnostics;
using System.IO;

namespace Writers
{
    public class TraceTextWriter : ActionTextWriter
    {
        public TraceTextWriter()
        {
            Actions.Add(s => Trace.Write(s));
        }
    }

    public class FileTextWriter : ActionTextWriter
    {
        public FileTextWriter(string path, bool append = false)
        {
            var sw = new StreamWriter(path, append) {AutoFlush = true};
            Actions.Add(sw.Write);
        }
    }
}

I agree that Linq to SQL Profiler is the right tool for this job. But if you don't want to spend the money or just need to do something simple, I like the DebugTextWriter approach.

After reading this question I went off looking for something more robust. It turns out Damien Guard also wrote a very nice article about building different writers to deal with different things like outputting to Memory, Debug, a File, Multiple Destinations, or even using simple Delegates.

I wound up using a couple of his ideas and writing an ActionTextWriter that can handle more than one delegate, and I thought I would share it here:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Writers
{
    public class ActionTextWriter : TextWriter
    {
        protected readonly List<Action<string>> Actions = new List<Action<string>>();

        public ActionTextWriter(Action<string> action)
        {
            Actions.Add(action);
        }

        public ActionTextWriter(IEnumerable<Action<string>> actions)
        {
            Actions.AddRange(actions);
        }

        public ActionTextWriter(params Action<string>[] actions)
        {
            Actions.AddRange(actions);
        }

        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Write(new string(buffer, index, count));
        }

        public override void Write(char value)
        {
            Write(value.ToString());
        }

        public override void Write(string value)
        {
            if (value == null)
            {
                return;
            }

            foreach (var action in Actions)
            {
                action.Invoke(value);
            }
        }
    }
}

You can add as many actions as you like. This example writes to a log file and the Console in Visual Studio via Debug.Write:

// Create data context
var fooDc = new FooDataContext();

// Create writer for log file.
var sw = new StreamWriter(@"C:\DataContext.log") {AutoFlush = true};

// Create write actions.
Action<string> writeToDebug = s => Debug.Write(s);
Action<string> writeToLog = s => sw.Write(s);

// Wire up log writers.
fooDc.Log = new ActionTextWriter(writeToDebug, writeToLog);

And of course if you want to make simpler ones to use off the cuff, you can always extend ActionTextWriter... write the generic approach and reuse, right?

using System.Diagnostics;
using System.IO;

namespace Writers
{
    public class TraceTextWriter : ActionTextWriter
    {
        public TraceTextWriter()
        {
            Actions.Add(s => Trace.Write(s));
        }
    }

    public class FileTextWriter : ActionTextWriter
    {
        public FileTextWriter(string path, bool append = false)
        {
            var sw = new StreamWriter(path, append) {AutoFlush = true};
            Actions.Add(sw.Write);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文