c# - 这两种方法有什么区别?是什么让第二个比第一个更好?

标签 c# asp.net string performance informix

我有两个方法做同样的事情。例如,当我在多个插入中使用它时,第一个会产生性能问题,有时会抛出 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/

相关文章:

c# - 尝试在 C# 中传递大量字符时尝试读取或写入 protected 内存

c# - 什么 c# 库用于圆度计软件的图形?

c# - 二进制流 'NN' 不包含有效的 BinaryHeader

asp.net - 允许引号字符作为 URL 参数的一部分有哪些风险?

javascript - 我需要帮助使用 VS-2013 在服务器上部署我的 asp.net 网站

asp.net - ASP.NET MVC View 模型的最佳实践

c++ - 生成按字典顺序大于输入的字符串

Python:使用 vars() 将字符串分配给变量

c# - 在 CsvHelper.CsvWriter 中手动添加标题

C++字符串和字符比较