Proper way to re-use OleDBCommand

  • Thread starter Thread starter sippyuconn
  • Start date Start date
S

sippyuconn

Hi

I have the code working - just want to make sure it is being coded correctly

I need to run thru a loop to insert multiple records thru Method
AddAuditMasterRecord
I have the OleDBCommand setup and was wondering if I am initializing it
correctly and doing cleanup to optimize performance

1)Should I run cmdMaster.Parameters.Clear(); after each call to
cmdMaster.ExecuteNonQuery(); or start Method AddAuditMasterRecord with
cmdMaster = new OleDbCommand(insertAuditDetailSQL, sqlCon); ??

2)I am useing IDisposible inteface to close connection on destcruction
Do I need to cleanup OleDBCommand ???

3)Anything else i should consider ???


Thanks
public MyClass : IDisposable
{

OleDbCommand cmdDetail ;
public MyClass()
{

sConn = "Correct Connection String";

sqlCon = new OleDbConnection(sConn);
sqlCon.Open();

cmdMaster = new OleDbCommand(insertAuditDetailSQL, sqlCon);
}

public void Dispose()
{

// CLEAN UP HERE!!!
CloseCommand();

}

private void CloseCommand()
{

sqlCon.Close();
}


public void AddAuditMasterRecord(int facilitykey, Guid visitid, int
audittypekey, string tablename, DateTime modifieddate)
{
//Parameters must be in order as in string insertAuditMasterSQL
cmdMaster.Parameters.Add("@Field1", OleDbType.Integer).Value =
facilitykey;
cmdMaster.Parameters.Add("@Field2", OleDbType.Guid).Value =
visitid;
OleDbType.Integer).Value = audittypekey;
cmdMaster.Parameters.Add("@Field3, OleDbType.VarChar).Value =
tablename;
cmdMaster.Parameters.Add("@Field4", OleDbType.DBTimeStamp).Value
= modifieddate;

cmdMaster.ExecuteNonQuery();
cmdMaster.Parameters.Clear();
}

}
 
sippyuconn said:
Hi

I have the code working - just want to make sure it is being coded correctly

I need to run thru a loop to insert multiple records thru Method
AddAuditMasterRecord
I have the OleDBCommand setup and was wondering if I am initializing it
correctly and doing cleanup to optimize performance

1)Should I run cmdMaster.Parameters.Clear(); after each call to
cmdMaster.ExecuteNonQuery(); or start Method AddAuditMasterRecord with
cmdMaster = new OleDbCommand(insertAuditDetailSQL, sqlCon); ??

2)I am useing IDisposible inteface to close connection on destcruction
Do I need to cleanup OleDBCommand ???

3)Anything else i should consider ???

The purpose of reusing a command is to set it up with parameters and
everything and then call the Prepare method. Then each time you use the
command you just assign values to the parameters and execute the command.

If you are going to add the parameters each time, there is no reason to
hang on to the command object either, you can just create a new command
object each time.
 
Hi

I have the code working - just want to make sure it is being coded correctly

I need to run thru a loop to insert multiple records thru Method
AddAuditMasterRecord
I have the OleDBCommand setup and was wondering if I am initializing it
correctly and doing cleanup to optimize performance

1)Should I run cmdMaster.Parameters.Clear(); after each call to
cmdMaster.ExecuteNonQuery(); or start Method AddAuditMasterRecord with
cmdMaster = new OleDbCommand(insertAuditDetailSQL, sqlCon); ??

2)I am useing IDisposible inteface to close connection on destcruction
Do I need to cleanup OleDBCommand ???

3)Anything else i should consider ???

Thanks
public MyClass : IDisposable
{

OleDbCommand cmdDetail ;
public MyClass()
{

sConn = "Correct Connection String";

sqlCon = new OleDbConnection(sConn);
sqlCon.Open();

cmdMaster = new OleDbCommand(insertAuditDetailSQL, sqlCon);
}

public void Dispose()
{

// CLEAN UP HERE!!!
CloseCommand();

}

private void CloseCommand()
{

sqlCon.Close();
}

public void AddAuditMasterRecord(int facilitykey, Guid visitid, int
audittypekey, string tablename, DateTime modifieddate)
{
//Parameters must be in order as in string insertAuditMasterSQL
cmdMaster.Parameters.Add("@Field1", OleDbType.Integer).Value =
facilitykey;
cmdMaster.Parameters.Add("@Field2", OleDbType.Guid).Value =
visitid;
OleDbType.Integer).Value = audittypekey;
cmdMaster.Parameters.Add("@Field3, OleDbType.VarChar).Value =
tablename;
cmdMaster.Parameters.Add("@Field4", OleDbType.DBTimeStamp).Value
= modifieddate;

cmdMaster.ExecuteNonQuery();
cmdMaster.Parameters.Clear();
}

}

You do not need to do a Parameters.Clear();
You just need to assign new values to the parameters:
command.Connection = session.Connection;
foreach (DataRow row in correspondenceTable.Rows) {

command.Parameters[0].Value = (int)row["ID"];
command.Parameters[1].Value = (int)row["Email"];
command.Parameters[2].Value = (int)row["Fax"];
command.Parameters[3].Value = (int)row["Save"];
command.ExecuteNonQuery();
}
 
Hello,

I see you marked this thread as answered in the web newsgroup. So I assume
the issue is resolved at this time. If you have any future questions or
concerns about the same problem, please feel free to update this thread and
I will try my best to do follow up.

Thanks for using Microsoft News Group Service!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top