原創|使用教程|編輯:郝浩|2013-04-26 17:29:32.000|閱讀 546 次
概述:在dotConnect for Oracle上如何使用OracleTrace組件呢?稍后將會用實例來展示。先來看一下OracleTrace組件,這個組件主要是用于跟蹤在服務器端執行的SQL和PL / SQL命令,SQL跟蹤位于服務器上的日志文件中,而L/SQL跟蹤位于數據庫表中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在dotConnect for Oracle上如何使用OracleTrace組件呢?稍后將會用實例來展示。先來看一下OracleTrace組件,這個組件主要是用于跟蹤在服務器端執行的SQL和PL / SQL命令,SQL跟蹤位于服務器上的日志文件中,而L/SQL跟蹤位于數據庫表中。
調用SqlTraceStart()方法或是SQL跟蹤開始時設置可用屬性為true,將會開啟SQL 和PL/SQL跟蹤,SQL跟蹤自動選擇跟蹤日志文件寫入跟蹤,通過的GetTraceFileName ()方法可以獲取該文件的完整路徑,使用MaxTraceFileSize屬性來限制日志文件的大小,SqlTraceMode屬性將會指定哪種類型的數據應在跟蹤的時候搜集。
PL / SQL跟蹤需要在服務器上安裝的dbms_trace包,PLSQL_TRACE_RUNS 和PLSQL_TRACE_EVENTS表創建來存儲日志,這個可以通過在Oracle數據庫/客戶端安裝包中執行tracetab.sql實現。第一個表中主要用于跟蹤運行時的信息,第二個表則要包括執行跟蹤后的事件描述。
調用SqlTraceStart()方法或是SQL跟蹤開始時設置可用屬性為true,開啟SQL 和PL/SQL跟蹤,也將會開啟SQL跟蹤。你需要權限來執行一個dbms_trace包用于開始PL / SQL跟蹤。通過PlSqlTracePause 和PlSqlTraceResume方法 ,PL/SQL跟蹤將會被暫停和恢復,PlSqlTraceLimit方法允許限制在數據庫中用于數據跟蹤的存儲容量,PlSqlTraceMode屬性將會指定跟蹤類型。
OracleTrace組件使用示例:
[C#]
// Initialize and open the connection to your Oracle server
// Connect as "system" to have the privileges needed to operate with the DBMS_TRACE package
OracleConnection connection = new OracleConnection();
connection.Server = "ORA";
connection.UserId = "system";
connection.Password = "password";
connection.ConnectMode = OracleConnectMode.SysDba;
connection.Open();
// Create the OracleTrace object representing the DBMS_TRACE package functionality.
OracleTrace trace = new OracleTrace(connection);
// Check the full path of the file where the trace log is stored
string path = trace.GetTraceFileName();
Console.WriteLine("The trace file: " + path);
// Create and execute a simple select query
// Observe that nothing was written to the trace file
OracleCommand command = new OracleCommand("select * from scott.dept", connection);
command.ExecuteReader();
// Start tracing the SQL commands execution.
trace.SqlTraceStart();
// Again execute the same command.
// The trace log contains now detailed information on the query performed.
command.ExecuteReader();
// Set the trace to observe the PL/SQL commands execution.
// The PL/SQL trace log is stored in the special database tables.
// By default, the PL/SQL trace mode is not set,
// thus we need to change it, e.g., to tracing all PL/SQL calls.
trace.PlSqlTraceMode = PlSqlTraceMode.AllCalls;
trace.PlSqlTraceStart();
// Now tracing for both SQL and PL/SQL execution is enabled.
// Note that this may be done by setting the Enabled property to true as well.
// Create an PL/SQL command.
string plsqlCode = @"
DECLARE
i INTEGER;
BEGIN
i:= 1;
FOR rec IN (SELECT DeptNo FROM Scott.Dept
WHERE RowNum <= 4 ORDER BY DeptNo)
LOOP
UPDATE Scott.Dept
SET DName = :NameArr(i)
WHERE DeptNo = Rec.DeptNo;
i:= i + 1;
END LOOP;
END;
";
string[] nameArray = { "Accounting", "Research", "Sales", "Operations" };
command = new OracleCommand(plsqlCode,connection);
command.Parameters.Add("NameArr", OracleDbType.VarChar);
command.Parameters["NameArr"].ArrayLength = 4;
[Visual Basic]
Dim connection As New OracleConnection
connection.Server = "ORA"
connection.UserId = "system"
connection.Password = "password"
connection.ConnectMode = 2
connection.Open()
Dim trace As New OracleTrace(connection)
Console.WriteLine(("The trace file: " & trace.GetTraceFileName))
Dim command As New OracleCommand("select * from scott.dept", connection)
command.ExecuteReader()
trace.SqlTraceStart()
command.ExecuteReader()
trace.PlSqlTraceMode = 1
trace.PlSqlTraceStart()
Dim plsqlCode As String = VbCrlf &
"DECLARE" & VbCrlf &
"i INTEGER;" & VbCrlf &
"BEGIN" & VbCrlf &
"i:= 1;" & VbCrlf &
"FOR rec IN (SELECT DeptNo FROM Scott.Dept" & VbCrlf &
" WHERE RowNum <= 4 ORDER BY DeptNo)" & VbCrlf &
"LOOP" & VbCrlf & " UPDATE Scott.Dept" & VbCrlf &
" SET DName = :NameArr(i)" & VbCrlf &
" WHERE DeptNo = Rec.DeptNo;" & VbCrlf &
" i:= i + 1;" & VbCrlf &
"END LOOP;" & VbCrlf &
"END;" & VbCrlf
Dim nameArray As String() = New String() {"Accounting", "Research", "Sales", "Operations"}
command = New OracleCommand(plsqlCode, connection)
command.Parameters.Add("NameArr", &H1C)
command.Parameters("NameArr").ArrayLength = 4
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都控件