【发布时间】:2012-01-30 11:26:26
【问题描述】:
我有两种方法可以做同样的事情。例如,当我在多个插入中使用它时,第一个会导致性能问题,有时会引发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;
}
【问题讨论】:
-
看起来你在重用某种形式的通用连接,OpenConnection();而不是让 .Net 框架管理连接池,即:使用 (Connection connection = new Connection(connString) { // db code here }
标签: c# asp.net string performance informix