Comments (6)
Here is the easiest way which I use.
Create a class which derives from Database, then override the OnExecutingCommand method.
public class MyDb : Database
{
public override void OnExecutingCommand(IDbCommand cmd)
{
File.WriteAllText("blah.txt", FormatCommand(cmd));
}
}
from npoco.
I found a function on PSC that combines the SQL and parameters and modified it to work in my database instance. I haven't made any changes to it yet other than changing the types to work with what NPoco uses. Pardon the VB.
Partial Public Class MyDB
Inherits NPoco.Database
Protected _lastCombinedSQL As String = String.Empty
Public ReadOnly Property LastCombinedSQL As String
Get
Return _lastCombinedSQL
End Get
End Property
Public Overrides Sub OnExecutingCommand(cmd As IDbCommand)
Try
_lastCombinedSQL = ConvertCommandParamatersToLiteralValues(cmd)
Catch ex As Exception
_lastCombinedSQL = "[error combining sql command and parameters]"
End Try
MyBase.OnExecutingCommand(cmd)
End Sub
''' <summary>
''' A Method to convert a IDbCommand with paramaters to string value.
''' </summary>
''' <param name="cmd"></param>
''' <returns></returns>
''' <remarks>
''' Based on http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7760&lngWId=10
''' </remarks>
Public Shared Function ConvertCommandParamatersToLiteralValues(cmd As IDbCommand) As String
If (cmd Is Nothing) Then Return String.Empty
Dim query As String = cmd.CommandText
For Each prm As IDbDataParameter In cmd.Parameters
Select Case prm.DbType
Case SqlDbType.Bit
Dim boolToInt As Integer = If(CBool(prm.Value), 1, 0)
query = query.Replace(prm.ParameterName, String.Format("{0}", If(CBool(prm.Value), 1, 0)))
Exit Select
Case SqlDbType.Int
query = query.Replace(prm.ParameterName, String.Format("{0}", prm.Value))
Exit Select
Case SqlDbType.VarChar
query = query.Replace(prm.ParameterName, String.Format("'{0}'", prm.Value))
Exit Select
Case Else
query = query.Replace(prm.ParameterName, String.Format("'{0}'", prm.Value))
Exit Select
End Select
Next
Return query
End Function
End Class
from npoco.
You could also use the latest Glimpse.Ado
package, which will be able to monitor all SQLs that are run. If you are running on Asp.net MVC you will need to install the Glimpse.Mvc4
or relevant version of MVC as well.
It will look like this.
from npoco.
This might be a good item to add to the Wiki, along with MiniProfiler support.
from npoco.
Thanks @techniq. Have put one together quickly.
https://github.com/schotime/NPoco/wiki/Debugging-and-Profiling
from npoco.
Perfect. Thanks.
from npoco.
Related Issues (20)
- No Tracking for Inserts HOT 1
- strongly typing IDs using implicit operator?
- One To Many References not working HOT 1
- Parameters are not working HOT 5
- InsertBulkAsync<T> fails with error: Cannot change DataType of a column once it has data. HOT 1
- new questions regarding multi-tenant apps
- Dependency injection example for context HOT 1
- SqliteConnection misdetected as SqlServerConnection HOT 1
- SQLite Insert Error : SQL logic error unrecognized token: @
- Hint() with PostgreSQL
- More than 4 Multiple Result Sets Mapping - 2
- Foreign key example using fluent mapping HOT 1
- Repository-wide license? HOT 1
- Why isn't DBConcurrencyException sent to IExceptionInterceptor?" HOT 1
- NullReferenceException / InvalidOperationException on multiple calls to Async Db method HOT 2
- Map string to json/jsonb field with PostgreSQL HOT 1
- Is version column in Sql Server supposed to be populated after an update?
- IMapper.GetParameterConverter sourceType parameter Is a different type between Insert and Update HOT 1
- NPOCO Does not map Composite Primary Key HOT 1
- culture bug??
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from npoco.