S
Steve
Microsoft's documentation of SqlCommand says that you can reuse you
SqlCommand by this:
You can reset the CommandText property and reuse the SqlCommand object.
However, you must close the SqlDataReader before you can execute a new
or previous command.
How do you do this if you don't get a handle on the SqlDataReader
object? I'm using ExecuteNonQuery rather than ExecuteReader. I can
see that ExecuteNonQuery calls ExecuteReader, but I never get to touch
the SqlDataReader to close it. If I hold onto the SqlCommand object
and reuse it, only ever doing an ExecuteNonQuery, I start getting many
different errors(object not found from null ref, DataReader already
exists, connection is closed for DataReader object). If I create a new
SqlDataReader every time, the database access works fine.
Is there any way I can reuse my object or am I stuck via the
limitations of ADO.NET?
Code snippets:
Reuse:
int numberAffectedRows = 0;
if(myScAudit == null) {
myScAudit = CreateNewCommand();
}
try {
myScAudit.Parameters.Clear();
myScAudit.Parameters.Add(new SqlParameter("@FileID", theFileID));
myScAudit.Parameters.Add(new SqlParameter("@UserID", theUserID));
myScAudit.Parameters.Add(new SqlParameter("@Description",
theDescription));
using(SqlConnection conn = DBConnection()) {
myScAudit.Connection = conn;
conn.Open();
numberAffectedRows = myScAudit.ExecuteNonQuery();
}
}
if(numberAffectedRows <= 0) {
throw new Exception("No rows affected for audit logging.");
}
}
catch (Exception e) {
if(log.IsErrorEnabled) {
log.Error("Audit logging failed for fileID " + theFileID.ToString()
+ " with log: " + theDescription, e);
}
}
New:
int numberAffectedRows = 0;
try {
using(SqlCommand scAudit = SetDBLogging()) {
scAudit.Parameters.Clear();
scAudit.Parameters.Add(new SqlParameter("@FileID", theFileID));
scAudit.Parameters.Add(new SqlParameter("@UserID", theUserID));
scAudit.Parameters.Add(new SqlParameter("@Description",
theDescription));
using(SqlConnection conn = DBConnection()) {
scAudit.Connection = conn;
conn.Open();
numberAffectedRows = scAudit.ExecuteNonQuery();
}
}
if(numberAffectedRows <= 0) {
throw new Exception("No rows affected for audit logging.");
}
}
catch (Exception e) {
if(log.IsErrorEnabled) {
log.Error("Audit logging failed for fileID " + theFileID.ToString()
+ " with log: " + theDescription, e);
}
}
Steve
SqlCommand by this:
You can reset the CommandText property and reuse the SqlCommand object.
However, you must close the SqlDataReader before you can execute a new
or previous command.
How do you do this if you don't get a handle on the SqlDataReader
object? I'm using ExecuteNonQuery rather than ExecuteReader. I can
see that ExecuteNonQuery calls ExecuteReader, but I never get to touch
the SqlDataReader to close it. If I hold onto the SqlCommand object
and reuse it, only ever doing an ExecuteNonQuery, I start getting many
different errors(object not found from null ref, DataReader already
exists, connection is closed for DataReader object). If I create a new
SqlDataReader every time, the database access works fine.
Is there any way I can reuse my object or am I stuck via the
limitations of ADO.NET?
Code snippets:
Reuse:
int numberAffectedRows = 0;
if(myScAudit == null) {
myScAudit = CreateNewCommand();
}
try {
myScAudit.Parameters.Clear();
myScAudit.Parameters.Add(new SqlParameter("@FileID", theFileID));
myScAudit.Parameters.Add(new SqlParameter("@UserID", theUserID));
myScAudit.Parameters.Add(new SqlParameter("@Description",
theDescription));
using(SqlConnection conn = DBConnection()) {
myScAudit.Connection = conn;
conn.Open();
numberAffectedRows = myScAudit.ExecuteNonQuery();
}
}
if(numberAffectedRows <= 0) {
throw new Exception("No rows affected for audit logging.");
}
}
catch (Exception e) {
if(log.IsErrorEnabled) {
log.Error("Audit logging failed for fileID " + theFileID.ToString()
+ " with log: " + theDescription, e);
}
}
New:
int numberAffectedRows = 0;
try {
using(SqlCommand scAudit = SetDBLogging()) {
scAudit.Parameters.Clear();
scAudit.Parameters.Add(new SqlParameter("@FileID", theFileID));
scAudit.Parameters.Add(new SqlParameter("@UserID", theUserID));
scAudit.Parameters.Add(new SqlParameter("@Description",
theDescription));
using(SqlConnection conn = DBConnection()) {
scAudit.Connection = conn;
conn.Open();
numberAffectedRows = scAudit.ExecuteNonQuery();
}
}
if(numberAffectedRows <= 0) {
throw new Exception("No rows affected for audit logging.");
}
}
catch (Exception e) {
if(log.IsErrorEnabled) {
log.Error("Audit logging failed for fileID " + theFileID.ToString()
+ " with log: " + theDescription, e);
}
}
Steve