简体   繁体   中英

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. I use generic DAL that handle MS SQL connection provider.

I found this query while searching 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

and many others in SO

This sql code can catch queries if they are executed from within SSMS (SQL server Management Studio)

but CANNOT catch queries if they are sent from my Ado.net application connected with MS sql database server.

My question:

Why I can't catch queries if they are sent from Ado.net based application using this code?

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)

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:

    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...

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.

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.

This technet article may be helpful to you: SQL Trace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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