小编use*_*504的帖子

多次打开/关闭一个 oracle 连接 c#

在我的业务逻辑中,我多次使用多个 oracle 查询。打开和关闭 oracle 连接的最佳方法是什么?

private void update()
{
     OracleConnection con = new OracleConnection("Connection Statement");
     OracleCommand command = new OracleCommand("Select Statement");
     con.Open();
     OracleDataReader reader = command.ExecuteReader();

     reader.Close();
     con.Close();

     // A for loop

     con.Open();
     command = new OracleCommand("Update statement");
     command.ExecuteNonQuery();
     con.Close();

     con.Open();
     command = new OracleCommand("Second Update statement");
     command.ExecuteNonQuery();
     con.Close();
}
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样。我应该为每个命令打开和关闭我的 oracle 连接,还是在第一个命令之前打开并在最后一个命令之后关闭。

PS 这个更新函数在我的应用程序中被调用了 100 多次。

c# oracle

2
推荐指数
1
解决办法
960
查看次数

SortedDictionary 与字典排序的性能对比

我有一个对象列表。这些对象具有许多属性,包括价格和数量。我需要创建一个带有键“价格”和值“数量”的新字典。如果两个对象具有相同的价格,则生成的字典应将价格作为键,将两个对象的数量总和作为值。据我所知,我可以通过两种方式做到这一点。

  1. 使用Dictionary数据结构,对最终的字典进行排序:
var result = new Dictionary<int, int>();
foreach(List<object> obj in list) {
    if(result.ContainsKey(obj.price)) {
        result[price] += quantity;
    }
    else {
        result[price] = quantity;
    }
}
result = result.OrderBy(x => x.Key);
Run Code Online (Sandbox Code Playgroud)
  1. 使用SortedDictionary
var result = new SortedDictionary<int, int>();
foreach(List<object> obj in list) {
    if(result.ContainsKey(obj.price)) {
        result[price] += quantity;
    }
    else {
        result[price] = quantity;
    }
}
Run Code Online (Sandbox Code Playgroud)

在第一种方法中,时间复杂度为 ,ContainsKey对于O(1)排序, order by 使用时间复杂度为 的快速排序O(nlogn)。所以总的时间复杂度是O(nlogn)。在第二种方法中,ContainsKeysortedDictionary 已经接受了O(log n),并且由于我 …

c# performance dictionary time-complexity sorteddictionary

2
推荐指数
1
解决办法
2489
查看次数

创建大小为 n 的 C# 列表并用 0 对其进行初始化

如何在 C# 中创建可变大小的列表并用 0 初始化所有元素?

我知道的一种方法是以这种方式使用 Enumerable

IEnumerable<int> list1 = Enumerable.Range(1, n).Select(x => x * 0);
Run Code Online (Sandbox Code Playgroud)

不确定使用这种方式的时间复杂度。只是寻找更好的方法(如果有的话)。

c#

0
推荐指数
1
解决办法
4989
查看次数