gpt4 book ai didi

c# - 在 C# 中使用 "using"的正确方法

转载 作者:行者123 更新时间:2023-12-03 14:35:12 25 4
gpt4 key购买 nike

<分区>

我有两组代码如下。我的问题是我们是否也需要对命令和适配器使用“using”还是仅用于连接?执行此操作的正确方法是什么?

 using (OracleConnection conn = new OracleConnection(ConnectionString))
{
conn.Open();
OracleCommand command = new OracleCommand(ProcedureName)
{
CommandType = CommandType.StoredProcedure,
Connection = conn
};
command.Parameters.Add(new OracleParameter("P_ID", OracleDbType.Varchar2, Id, ParameterDirection.Input));
command.Parameters.Add(new OracleParameter("CUR_OUT", OracleDbType.RefCursor, ParameterDirection.Output));

OracleDataAdapter adapter = new OracleDataAdapter(command);
adapter.Fill(dt);
}

上面的代码只有一个using语句,下面的代码有3个using语句

 using (OracleConnection conn = new OracleConnection(ConnectionString))
{
await conn.OpenAsync();
using (OracleCommand oCommand = new OracleCommand(ProcedureName))
{
oCommand.BindByName = true;
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandTimeout = await GetCommandTimeout(DatabaseConstants.OracleCommandTimeoutInSeconds);
oCommand.Connection = conn;

oCommand.Parameters.Add(new OracleParameter("P_ID", OracleDbType.Varchar2, Id, ParameterDirection.Input));
oCommand.Parameters.Add(new OracleParameter("CUR_OUT", OracleDbType.RefCursor, ParameterDirection.Output));
using (OracleDataAdapter oDatAdapter = new OracleDataAdapter(oCommand))
{
oDatAdapter.Fill(dataSet);
}
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com