简体   繁体   English

从ado.net应用程序(MS SQL提供程序)发送到服务器的什么sql语句

[英]what sql statement sent to server from ado.net application (MS SQL provider)

I want to know what sql statement sent to the server from my ado.net based application for loging exceptions and debugging. 我想知道从基于ado.net的应用程序发送到服务器的sql语句,用于记录异常和调试。 I use generic DAL that handle MS SQL connection provider. 我使用处理MS SQL连接提供程序的通用DAL。

I found this query while searching SO: 我在搜索SO时发现了此查询:

SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC

reference: SQL Server Query Trace 参考: SQL Server查询跟踪

and many others in SO 还有很多其他的

This sql code can catch queries if they are executed from within SSMS (SQL server Management Studio) 如果这些SQL代码是在SSMS(SQL Server Management Studio)中执行的,则可以捕获查询

but CANNOT catch queries if they are sent from my Ado.net application connected with MS sql database server. 但是如果它们是从与MS sql数据库服务器连接的Ado.net应用程序发送的,则无法捕获查询。

My question: 我的问题:

Why I can't catch queries if they are sent from Ado.net based application using this code? 如果使用此代码从基于Ado.net的应用程序发送查询,为什么不能捕获查询?

Is there other way (sql code/ c# code) to catch the real sql statements sent from my application (like what i get in sql profiller) 还有其他方法(sql代码/ c#代码)来捕获从我的应用程序发送的真实sql语句(例如我在sql profiller中获得的内容)

If your DAL is using the abstract IDbConnection and IDbCommand interfaces, and you have a common factory for instantiating your IDbConnection, you could create your own implementation of IDbConnection that overrides CreateCommand to return your own implementation of IDbCommand which logs ExecuteReader, ExecuteNonQuery, and ExecuteScalar before calling the real underlying objects: 如果您的DAL使用抽象的IDbConnection和IDbCommand接口,并且您有一个通用的实例化IDbConnection的工厂,则可以创建自己的IDbConnection实现,该实现将覆盖CreateCommand以返回您自己的IDbCommand实现,该实现将在之前记录ExecuteReader,ExecuteNonQuery和ExecuteScalar调用真正的基础对象:

    public class MyDbConnection : IDbConnection {
        private IDbConnection src;
        public MyDbConnection(IDbConnection src) {
            this.src = src;
        }
        public IDbCommand CreateCommand() {
            return new MyDbCommand(src.CreateCommand());
        }
        //TODO create pass-through implementations of the rest of IDbConnection methods...
    }

    public class MyDbCommand : IDbCommand {
        private IDbCommand src;
        public MyDbCommand(IDbCommand src) {
            this.src = src;
        }

        public int ExecuteNonQuery() {
            log.Info(src.CommandText);
            return src.ExecuteNonQuery();
        }

        public IDataReader ExecuteReader() {
            log.Info(src.CommandText);
            return src.ExecuteReader();
        }

        public IDataReader ExecuteReader(CommandBehavior behavior) {
            log.Info(src.CommandText);
            return src.ExecuteReader(behavior);
        }

        public object ExecuteScalar() {
            log.Info(src.CommandText);
            return src.ExecuteScalar();
        }

        //TODO create pass-through implementations of the rest of IDbCommand methods and properties...
    }

Now, wherever you were creating your IDbConnection, return new MyDbConnection(theRealDbConnection) instead... 现在,无论您在哪里创建IDbConnection,都应返回新的MyDbConnection(theRealDbConnection)。

Ah, sorry, I misunderstood the question. 啊,对不起,我误解了这个问题。 My previous answer showed how to capture what your app was sending to the sql client, not what the sql client was sending to the server. 我之前的答案显示了如何捕获应用程序发送给sql客户端的内容,而不是sql客户端发送给服务器的内容。

In order to capture SQL statements that are sent by the SQL client to the server, you will need to use sp_trace_create and related trace functions to configure sql server to send trace information back to your application. 为了捕获SQL客户端发送到服务器的SQL语句,您将需要使用sp_trace_create和相关的跟踪函数来配置sql服务器,以将跟踪信息发送回您的应用程序。

This technet article may be helpful to you: SQL Trace 此technet文章可能对您有所帮助: SQL跟踪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM