I have two methods that do the same thing. The first one makes performance problems when I use it in multiple insert, for example, and sometimes throws an Input string was in incorrect format exception. The second one works okay. I want to know why this first one has these problems and what’s the difference between the two methods.
First Method:
public DataTable Return_DataTable(string cmdText, CommandType cmdType,
Dictionary<string, string> Param_arr)
{
Open_Connection();
int return_val = -1;
DataTable dt = new DataTable();
command.CommandText = cmdText;
command.CommandType = cmdType;
if (cmdType == CommandType.StoredProcedure)
{
if (Param_arr != null)
{
command.Parameters.Clear();
if (Param_arr.Count > 0)
{
for (IEnumerator<KeyValuePair<string, string>> enumerator =
Param_arr.GetEnumerator(); enumerator.MoveNext(); )
{
param = command.CreateParameter();
param.ParameterName = enumerator.Current.Key.ToString();
param.Value = enumerator.Current.Value.ToString();
command.Parameters.Add(param);
}
}
}
}
IfxDataReader dr2;
try
{
dr2 = command.ExecuteReader();
dt.Load(dr2);
}
catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
{
ErrMappingForInformix.WriteLog("\r\n Error Code: " +
ifxEx.Errors[0].NativeError.ToString() +
"\r\n MEssage: " + ifxEx.Errors[0].Message);
throw new Exception("ERROR:" + ifxEx.Errors[0].NativeError.ToString() +
"\r\n MEssage: " + ifxEx.Errors[0].Message);
}
catch (Exception ex)// Handle all other exceptions.
{
ErrMappingForInformix.WriteLog("\r\n Error Message: " + ex.Message);
throw new Exception("\r\n Error Message: " + ex.Message);
}
finally
{
Close_Connection();
}
return dt;
}
Second Method :
public DataTable Return_DataTable(IfxCommand cmd, CommandType cmdtype,
IfxParameter[] parameters)
{
Open_Connection();
DataTable dt = new DataTable();
cmd.CommandText = cmd.CommandText;
cmd.CommandType = cmdtype;
cmd.Connection = connection;
cmd.CommandTimeout = 100;
for (int j = 0; j < parameters.Length; j++)
{
cmd.Parameters.Add(parameters[j]);
}
try
{
dt.Load(cmd.ExecuteReader());
}
catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
{
ErrMappingForInformix.WriteLog("\r\n Error Code: " +
ifxEx.Errors[0].NativeError.ToString() +
"\r\n MEssage: " + ifxEx.Errors[0].Message);
throw new Exception("ERROR:" + ifxEx.Errors[0].NativeError.ToString() +
"\r\n MEssage: " + ifxEx.Errors[0].Message);
}
catch (Exception ex)// Handle all other exceptions.
{
ErrMappingForInformix.WriteLog("\r\n Error Message: " + ex.Message);
throw new Exception("\r\n Error Message: " + ex.Message);
}
finally
{
Close_Connection();
}
return dt;
}
The second method handles the parameters passed to the procedure as strongly-typed IfxParameter objects (which I assume inherit from dbParameter). The first method stores the same data as a pair of strings, which is a much poorer representation; it contains less metadata- for example the data type of the value is not retained.
The DbDataParameter includes the following information:
The first method which provides only the name and a string representation of the value is forcing ADO.NET to attempt to cast the string to the correct datatype, and either use default values or look at the available metadata to figure out the right values for the other parameters (I don’t remember off the top of my head what the exact behavior is).