R
Reddy
Hi,
I am using .NET 1.1 on Win2k and XP and SQLServer 2000. I am using
DataReader to read some data (say 100 rows) using
"SELECT ..... FROM .... WHERE ...."
(not a stored procedure). Since my query is a simple select, I am not
modifying any of the data using the query. In *SOME* of my queries
it is taking significantly long time (2 seconds compared to few milli
seconds) to close the data reader. As suggested in the .NET
documentation I am cancelling the command before closing the reader.
I am getting intermittent exceptions that says
"System.Data.SqlClient.SqlException: Operation cancelled by user."
But I am cancelling my command only after reading all my data.
Why is it throwing this exception at that point? This exception comes
for the first time I run my application where the connection has to
be established with database.
Sometimes it comes up even after the connection is established. It
is totally random.
Can somebody shed some light on this extended delay in closing
readers and cancelling commands.
thanks,
- Reddy
-------------------------------------------------------------------------------
SqlDataReader.Close() help:
------------------------------
"The Close method fills in the values for output parameters,
return values and RecordsAffected, increasing the amount of time
it takes to close a SqlDataReader that was used to process a large
or complicated query. In cases where the return values and the
number of records affected by a query are not significant, the
amount of time it takes to close the SqlDataReader can be reduced
by calling the Cancel method of the associated SqlCommand object
before calling the Close method."
-------------------------------------------------------------------------------
...
dataCommand.CommandText = "SELECT CUSTOMER_NAME, CITY FROM CUSTOMER
WHERE CUSTOMER_ID > @CUSTOMER_ID";
IDbDataParameter param = datacommand.CreateParameter();
param.ParameterName = "@CUSTOMER_ID";
param.ParameterValue = 5;
dataCommand.Parameters.Add(param)
IDataReader reader = dataCommand.ExecuteReader();
while(reader.Read())
{
myArrayList.Add(new CustomerInfo(reader.GetString(0),
reader.GetString(2)));
}
dataCommand.Cancel();
dataCommand.Dispose(); // I tried without this line as weel. No
luck.
reader.Close();
reader = null;
dataCommand = null;
transaction = connection.BeginTransaction(); // <--------
exception thrown here
dataCommand = connection.CreateCommand();
dataCommand.Transaction = transaction;
...
...
...
-------------------------------------------------------------------------------
Exception: System.Data.SqlClient.SqlException
Message: Operation cancelled by user.
Source: .Net SqlClient Data Provider
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.ThrowAttentionError()
at System.Data.SqlClient.TdsParser.ProcessDone(SqlCommand cmd,
RunBehavior run)
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand
cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.TdsParser.Run(RunBehavior run)
at System.Data.SqlClient.SqlInternalConnection.ExecuteTransaction(String
sqlBatch, String method)
at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel
iso)
at System.Data.SqlClient.SqlConnection.BeginTransaction()
at System.Data.SqlClient.SqlConnection.System.Data.IDbConnection.BeginTransaction()
at (my code line #....)
-------------------------------------------------------------------------------
I am using .NET 1.1 on Win2k and XP and SQLServer 2000. I am using
DataReader to read some data (say 100 rows) using
"SELECT ..... FROM .... WHERE ...."
(not a stored procedure). Since my query is a simple select, I am not
modifying any of the data using the query. In *SOME* of my queries
it is taking significantly long time (2 seconds compared to few milli
seconds) to close the data reader. As suggested in the .NET
documentation I am cancelling the command before closing the reader.
I am getting intermittent exceptions that says
"System.Data.SqlClient.SqlException: Operation cancelled by user."
But I am cancelling my command only after reading all my data.
Why is it throwing this exception at that point? This exception comes
for the first time I run my application where the connection has to
be established with database.
Sometimes it comes up even after the connection is established. It
is totally random.
Can somebody shed some light on this extended delay in closing
readers and cancelling commands.
thanks,
- Reddy
-------------------------------------------------------------------------------
SqlDataReader.Close() help:
------------------------------
"The Close method fills in the values for output parameters,
return values and RecordsAffected, increasing the amount of time
it takes to close a SqlDataReader that was used to process a large
or complicated query. In cases where the return values and the
number of records affected by a query are not significant, the
amount of time it takes to close the SqlDataReader can be reduced
by calling the Cancel method of the associated SqlCommand object
before calling the Close method."
-------------------------------------------------------------------------------
...
dataCommand.CommandText = "SELECT CUSTOMER_NAME, CITY FROM CUSTOMER
WHERE CUSTOMER_ID > @CUSTOMER_ID";
IDbDataParameter param = datacommand.CreateParameter();
param.ParameterName = "@CUSTOMER_ID";
param.ParameterValue = 5;
dataCommand.Parameters.Add(param)
IDataReader reader = dataCommand.ExecuteReader();
while(reader.Read())
{
myArrayList.Add(new CustomerInfo(reader.GetString(0),
reader.GetString(2)));
}
dataCommand.Cancel();
dataCommand.Dispose(); // I tried without this line as weel. No
luck.
reader.Close();
reader = null;
dataCommand = null;
transaction = connection.BeginTransaction(); // <--------
exception thrown here
dataCommand = connection.CreateCommand();
dataCommand.Transaction = transaction;
...
...
...
-------------------------------------------------------------------------------
Exception: System.Data.SqlClient.SqlException
Message: Operation cancelled by user.
Source: .Net SqlClient Data Provider
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.ThrowAttentionError()
at System.Data.SqlClient.TdsParser.ProcessDone(SqlCommand cmd,
RunBehavior run)
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand
cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.TdsParser.Run(RunBehavior run)
at System.Data.SqlClient.SqlInternalConnection.ExecuteTransaction(String
sqlBatch, String method)
at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel
iso)
at System.Data.SqlClient.SqlConnection.BeginTransaction()
at System.Data.SqlClient.SqlConnection.System.Data.IDbConnection.BeginTransaction()
at (my code line #....)
-------------------------------------------------------------------------------