I have written the following method twice but I don't know which is better from performance perspective, code design and best practice.
First:
public int Insert()
{
int affectedRow = -1;
using (IfxConnection con = new IfxConnection(ConfigurationManager.ConnectionStrings["sh"].ToString()))
{
StringBuilder cmdTxt = new StringBuilder();
cmdTxt.Append(" INSERT INTO shedule(day,short,name,depcode,studycode,batchnum) VALUES (?,?,?,?,?,?) ");
using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
{
myIfxCmd.CommandType = CommandType.Text;
myIfxCmd.Parameters.Add("day", IfxType.Char);
myIfxCmd.Parameters.Add("short", IfxType.NVarChar);
myIfxCmd.Parameters.Add("name", IfxType.NVarChar);
myIfxCmd.Parameters.Add("depcode", IfxType.Integer);
myIfxCmd.Parameters.Add("studycode", IfxType.Integer);
myIfxCmd.Parameters.Add("batchnum", IfxType.Integer);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
myIfxCmd.Parameters[0].Value = ((object)this.DayId) ?? DBNull.Value;
myIfxCmd.Parameters[1].Value = ((object)this.ShortName) ?? DBNull.Value;
myIfxCmd.Parameters[2].Value = ((object)this.Name) ?? DBNull.Value;
myIfxCmd.Parameters[3].Value = this.DepCode;
myIfxCmd.Parameters[4].Value = this.StudyCode;
myIfxCmd.Parameters[5].Value = this.BatchNum;
affectedRow = myIfxCmd.ExecuteNonQuery();
}
}
return affectedRow;
}
THEN :
foreach (Day a in days)
{
affectedRow = a.Insert();
}
Second:
public int Insert(List<Day> days)
{
int affectedRow = -1;
using (IfxConnection con = new IfxConnection(ConfigurationManager.ConnectionStrings["sh"].ToString()))
{
StringBuilder cmdTxt = new StringBuilder();
cmdTxt.Append(" INSERT INTO shedule(day,short,name,depcode,studycode,batchnum) VALUES (?,?,?,?,?,?) ");
using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
{
myIfxCmd.CommandType = CommandType.Text;
myIfxCmd.Parameters.Add("day", IfxType.Char);
myIfxCmd.Parameters.Add("short", IfxType.NVarChar);
myIfxCmd.Parameters.Add("name", IfxType.NVarChar);
myIfxCmd.Parameters.Add("depcode", IfxType.Integer);
myIfxCmd.Parameters.Add("studycode", IfxType.Integer);
myIfxCmd.Parameters.Add("batchnum", IfxType.Integer);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
foreach (Day a in days)
{
myIfxCmd.Parameters[0].Value = ((object)a.DayId) ?? DBNull.Value;
myIfxCmd.Parameters[1].Value = ((object)a.ShortName) ?? DBNull.Value;
myIfxCmd.Parameters[2].Value = ((object)a.Name) ?? DBNull.Value;
myIfxCmd.Parameters[3].Value = a.DepCode;
myIfxCmd.Parameters[4].Value = a.StudyCode;
myIfxCmd.Parameters[5].Value = a.BatchNum;
affectedRow = myIfxCmd.ExecuteNonQuery();
}
}
}
return affectedRow;
}