private string sqlLog = "";
void Page_Load() {
DbSession.Default.RegisterSqlLogger(OnSqlLog);
}
void OnSqlLog(string logMsg) {
sqlLog += "
" + logMsg; // 累積顯示於頁面
}
// 執行查詢...
// 最後取消註冊
DbSession.Default.UnregisterSqlLogger(OnSqlLog);
DbSession.Default.From()
.InnerJoin(Suppliers._.SupplierID == Products._.SupplierID)
.Page(10, 2)
.Select(Products._.ProductID, Products._.ProductName)
.ToDataTable();
SELECT * FROM (
SELECT TOP 10 * FROM (
SELECT TOP 20 [Products].[ProductID], [Products].[ProductName]
FROM [Products]
INNER JOIN [Suppliers] ON ([Suppliers].[SupplierID] = [Products].[SupplierID])
ORDER BY [Products].[ProductID] ASC
) AS tempIntable ORDER BY [ProductID] DESC
) AS tempOuttable ORDER BY [ProductID] ASC
DbSession.Default.From().Where(Products._.CategoryID == 2).ToFirst();
Text:
SELECT TOP 1 * FROM [Products] WHERE [Products].[CategoryID] = @p0
Parameters:
@p0 [Int32] = 2
@p0, @p1... 佔位)。DbSession.Default.RegisterSqlLogger((sql, elapsedMs) => {
if (elapsedMs > 200) Log4net.Warn($"Slow SQL ({elapsedMs}ms): {sql}");
});
(需組件支援帶執行時間的多載,舊版僅傳 SQL 字串。)
SQL 輸出功能讓 ORM 黑盒透明化,是 Hxj.Data 開發除錯的核心利器。