拦截数据库查询命令 [英] Intercepting Database Query Commands

查看:116
本文介绍了拦截数据库查询命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个Microsoft SQL Server数据库,并且正在使用LINQ to SQL在ASP.Net C#4.0网站中与它进行交互.我想做的是提供一种方法,在将查询提交到数据库之前,先获取将要发送的命令并允许我查看它.基本上,我试图查看命令/请求的效率,以便我可以发出更大的请求,而不是许多小的请求.我意识到可以使用Microsoft SQL Server工具来完成此操作,但实际上我没有访问数据库服务器本身的权限,只能访问我的代码的调试工具.

我尝试覆盖DataContext的SubmitChanges方法,以查看是否可以从其中获取传出命令的任何方式,但我似乎找不到任何方式.我也查看了GetCommand方法,但我不想遍历所有50,000行代码,将GetCommand方法放在站点中的每个LINQ语句上……而且,这些不会向我显示实际的传出命令,如下所示:它们将包含缓存的对象.

我想做的事可能吗?谁能提供有关该方法的任何见解?

非常感谢,
Ed

Hi all,

I have a Microsoft SQL Server database and I am using LINQ to SQL to interact with it in my ASP.Net C# 4.0 Website. What I would like to do is have a method that before a query is submitted to the database, grabs the command that will be sent and allows me to look at it. Basically, I am trying to see how efficient my commands/requests are so that I can make more bigger requests rather than lots of small ones. I realise this can be done using Microsoft SQL Server tools but I don''t actually have access the the database server itself, only debugging tools for my code.

I have tried overriding the SubmitChanges method of my DataContext to see if there was any way I could get at the outgoing commands from within that but I can''t seem to find one. I also looked at the GetCommand method, but I don''t want to have to go through all my 50,000 lines of code putting GetCommand methods on every LINQ statement in site... Furthermore, these would not show me the actual outgoing commands as they would include cached objects.

Is what I''m trying to do possible? Can anyone offer any insight into how to do it?

Thanks very much,
Ed

推荐答案

非常感谢amitgajjar提出的解决方案.

此解决方案使用内置的DataContext.Log属性.由于在我的情况下,我是作为网站运行的,因此可能存在多个DataContext,因此我使用静态方法创建了所有DataContext并将我想要的静态StringWriter分配给它们的Log属性用于日志.代码如下:

Thanks greatly to amitgajjar for this solution.

This solution use the in-built DataContext.Log property. Since in my situation I am running this as a website and so multiple DataContexts may exist, I used a static method to create all my DataContexts and assign to their Log property the static StringWriter I wanted to use for the log. The code looked as follows:

public static class DatabaseHandler
{
    public static System.IO.StringWriter LogStream = new System.IO.StringWriter();

    internal static DatabaseDataContext CreateDatabaseContext()
    {
        DatabaseDataContext TheContext = new DatabaseDataContext();

        TheContext.Log = LogStream;

        return TheContext;
    }
}



然后,我有了一个aspx页面,只需将其输出设置为键入文本",然后执行DatabaseHandler.LogStream.GetStringBuilder().ToString()即可获取日志.

这是一个非常有效且简洁的解决方案,再次感谢amitgajjar.

希望这可以帮助其他有需要的人,
Ed



And then I had an aspx page that simply set it''s ouput to type "text" and did DatabaseHandler.LogStream.GetStringBuilder().ToString() to get the log.

This is a very efficient and neat solution, thanks so much again to amitgajjar.

Hope this helps anyone else in need,
Ed


这篇关于拦截数据库查询命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆