SqlDataAdapter with using keyword(带有 using 关键字的 SqlDataAdapter)
问题描述
下面的代码健康吗?或者我不需要使用 using
关键字,因为 SqlDataAdapter
将处理关闭连接?
Is this the following code healthy? Or I don't need to use the using
keyword as the SqlDataAdapter
will handle closing the connection?
public static DataSet Fetch(string sp, SqlParameter [] prm)
{
using (SqlConnection con = new SqlConnection(ConStr))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Parameters.AddRange(prm);
using (SqlDataAdapter dta = new SqlDataAdapter(cmd))
{
DataSet dst = new DataSet();
dta.Fill(dst);
return dst;
}
}
}
}
<小时><小时>
@MarkGravell 我在这里需要一个建议,我真的很想使用 DataReader
,但我一直在寻找使用 using
关键字来确保关闭连接.DataReader
我们不能使用它,因为如果我们想将 DataReader
返回到某个方法,它将关闭连接.那么您认为以下技术适用于 DataReader
和 using
关键字:
@MarkGravell I need a suggestions here, I am really looking to use DataReader
, but I was looking all the time to use the using
keyword to ensure closing the connections. Where with DataReader
we can not use it because it will close the connection if we want to return the DataReader
back to some method.
So do you think the following technique is fine with DataReader
and the using
keyword:
public static SqlDataReader Fetch(string sp, SqlParameter [] prm)
{
SqlCommand cmd = new SqlConnection(ConStr).CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sp;
cmd.Parameters.AddRange(prm);
cmd.Connection.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
using (SqlDataReader dtrPrize = Sql.Fetch("SelectPrize", new SqlParameter[] { new SqlParameter("id", id) }))
{
dtrPrize.Read();
Prize prize = new Prize();
prize.id = (int)dtrPrize[dtrPrize.GetOrdinal("id")];
prize.artitle = (string)dtrPrize[dtrPrize.GetOrdinal("artitle")];
prize.entitle = (string)dtrPrize[dtrPrize.GetOrdinal("entitle")];
prize.ardetail = (string)dtrPrize[dtrPrize.GetOrdinal("ardetail")];
prize.endetail = (string)dtrPrize[dtrPrize.GetOrdinal("endetail")];
prize.image = (string)dtrPrize[dtrPrize.GetOrdinal("image")];
prize.theme = (string)dtrPrize[dtrPrize.GetOrdinal("theme")];
prize.price = (int)dtrPrize[dtrPrize.GetOrdinal("price")];
prize.audience = (int)dtrPrize[dtrPrize.GetOrdinal("audience")];
prize.type = (byte)dtrPrize[dtrPrize.GetOrdinal("type")];
prize.status = (byte)dtrPrize[dtrPrize.GetOrdinal("status")];
prize.voucher = (string)dtrPrize[dtrPrize.GetOrdinal("voucher")];
prize.supplierid = (int)dtrPrize[dtrPrize.GetOrdinal("supplierid")];
prize.created = (DateTime)dtrPrize[dtrPrize.GetOrdinal("created")];
prize.updated = (DateTime)dtrPrize[dtrPrize.GetOrdinal("updated")];
return prize;
}
推荐答案
Healthy-ish;我个人认为不健康的一点是它使用了 DataSet
和 DataAdapter
,但这可能只是我个人的偏见.
Healthy-ish; personally I'd say the unhealthy bit is the bit where it makes use of DataSet
and DataAdapter
, but that is perhaps just my personal bias.
是的,你应该在这里处理适配器等(这显然是 using
为你做的).
Yes, you should dispose the adapter etc here (which is what the using
does for you, obviously).
作为一个琐碎无意义的整理,您可以堆叠 使用
s - 只是让它不那么冗长:
As a trivial pointless tidy, you can stack the using
s - just makes it a little less verbose:
using (SqlConnection con = new SqlConnection(ConStr))
using (SqlCommand cmd = con.CreateCommand())
{
这篇关于带有 using 关键字的 SqlDataAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 using 关键字的 SqlDataAdapter
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01