Re: Using parameters: Get SQL sent to database
- From: luxspes <me@xxxxxxxxxxx>
- Date: Tue, 22 Nov 2005 09:22:17 -0600
luxspes wrote:
Armin Zingler wrote:
(A Gentle Introduction to ADO.NET 2.0 Trace Facilities):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/tracingdataaccess.asp
Copy&Pasted from (A Gentle Introduction to ADO.NET 2.0 Trace Facilities):
Using Tracing to Debug a Parameter Binding Problem
Now that we've gone through the quick overview of tracing, I'd like to present a simple use case. I'd often use ODBC trace to do problem determination when an application would "eat" a rich error message and produce a polite but fairly "information-free" message. Such application code would look like this:
string s = GetConnectionStringFromConfigFile();
using (SqlConnection conn = new SqlConnection(s))
using (SqlCommand cmd = new SqlCommand(
"select * from authors where au_id = @auid", conn))
{
// the error is hardcoded here but could have come from suboptimal
// editing in a graphic user interface
cmd.Parameters.Add("@auid", SqlDbType.Int);
cmd.Parameters[0].Value = 123456789;
SqlDataReader rdr = null;
try {
// some code that could fail goes here
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
Console.WriteLine(rdr[0]);
rdr.Close();
}
catch (Exception e) {
MessageBox.Show("polite error message");
}
}In this case, the error was caused by a parameter type mismatch, and the person diagnosing the error might not have access to the source code of the program. Turning on the trace, we'll see output like this:
"enter_01 <sc.SqlCommand.ExecuteReader|API> 1#" "<sc.SqlCommand.get_Connection|API> 1#" "<sc.SqlCommand.get_Connection|API> 1#" "<sc.TdsParser.CreateSession|ADV> 1# created session 3" "<sc.TdsParserSessionPool.CreateSession|ADV> 1# adding session 3 to pool" "<sc.TdsParserSessionPool.GetSession|ADV> 1# using session 3" "<sc.TdsParser.GetSession|ADV> 1# getting session 3 from pool" "<sc.SqlCommand.ExecuteReader|INFO> 1# Command executed as RPC." "<sc.SqlCommand.get_Connection|API> 1#" "leave_01" "enter_01 <sc.SqlDataReader.Read|API> 1#" "<sc.SqlError.SqlError|ERR> infoNumber=245 errorState=1 errorClass=16 errorMessage='Syntax error converting the varchar value '172-32-1176' to a column of data type int.' procedure='' lineNumber=1" "leave_01"
This shows us directly that there is a parameter value mismatch. The sample and the trace file are provided in the article code. Note that the trace file is much more compact in this case because we're only tracing with the System.Data.1 provider.
.
- Follow-Ups:
- Re: Using parameters: Get SQL sent to database
- From: Cor Ligthert [MVP]
- Re: Using parameters: Get SQL sent to database
- References:
- Using parameters: Get SQL sent to database
- From: Armin Zingler
- Re: Using parameters: Get SQL sent to database
- From: luxspes
- Using parameters: Get SQL sent to database
- Prev by Date: Re: Using parameters: Get SQL sent to database
- Next by Date: Re: Using parameters: Get SQL sent to database
- Previous by thread: Re: Using parameters: Get SQL sent to database
- Next by thread: Re: Using parameters: Get SQL sent to database
- Index(es):
Relevant Pages
|
Loading