我有两个方法做同样的事情。例如,当我在多个插入中使用它时,第一个会产生性能问题,有时会抛出 Input string was in incorrect format
异常。第二个工作正常。我想知道为什么第一个有这些问题,这两种方法有什么区别。
第一种方法:
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;
}
第二种方法:
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;
}
最佳答案
第二种方法将传递给过程的参数作为强类型 IfxParameter 对象(我假设它继承自 dbParameter)进行处理。第一种方法将相同的数据存储为一对字符串,这是一种较差的表示;它包含较少的元数据——例如,不保留值的数据类型。
DbDataParameter 包括以下信息:
DbType DbType { get; set; }
ParameterDirection Direction { get; set; }
bool IsNullable { get; }
string ParameterName { get; set; }
string SourceColumn { get; set; }
DataRowVersion SourceVersion { get; set; }
object Value { get; set; }
byte Precision { get; set; }
byte Scale { get; set; }
int Size { get; set; }
第一种方法仅提供名称和值的字符串表示,强制 ADO.NET 尝试将字符串转换为正确的数据类型,并使用默认值或查看可用的元数据来确定正确的数据类型其他参数的值(我不记得确切的行为是什么)。
关于c# - 这两种方法有什么区别?是什么让第二个比第一个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9063101/