最佳实践?打开和关闭多个连接,或 ado.net 的一个大型打开连接
我正在使用 ADO.Net 进行某些数据库连接,我只是希望我以正确的方式进行操作。我正在为每个存储过程打开和关闭多个连接。或者我应该将其包装在一个开放连接中(可能数据库资源较少?)谢谢,如果有任何奇怪的事情或我可以做得更好的事情请告诉我,谢谢!
示例:这个,但我有大约 6 个......
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTrip", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
ddlTripTypeA.DataSource = cmd.ExecuteReader();
ddlTripTypeA.DataTextField = "TripType";
ddlTripTypeA.DataValueField = "TripTypeAID";
ddlTripTypeA.DataBind();
}
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTripB", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
ddlTripTypeB.DataSource = cmd.ExecuteReader();
ddlTripTypeB.DataTextField = "TripType";
ddlTripTypeB.DataValueField = "TripTypeBID";
ddlTripTypeB.DataBind();
}
I am using ADO.Net for some database connectivity and I was just hoping I was doing it the right way. I am opening and closing multiple connections for each stored procedure. Or should I be wrapping this up in just one open connection (less resource on the database maybe?) thanks, and if there is anything odd or something I could be doing better let me know thanks!
Example: this but I have like 6 of them...
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTrip", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
ddlTripTypeA.DataSource = cmd.ExecuteReader();
ddlTripTypeA.DataTextField = "TripType";
ddlTripTypeA.DataValueField = "TripTypeAID";
ddlTripTypeA.DataBind();
}
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spSelectAllTripB", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
ddlTripTypeB.DataSource = cmd.ExecuteReader();
ddlTripTypeB.DataTextField = "TripType";
ddlTripTypeB.DataValueField = "TripTypeBID";
ddlTripTypeB.DataBind();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.Net 已经为您管理了连接池,因此您无需像使用旧的 asp 那样担心重用连接。我总是进行几个小的快速呼叫,而不是一直保持一个打开状态,因为并非所有打开的时间都通常用于呼叫。您的站点代码也正在运行,并在两者之间执行一些操作。
现在,如果您要一个接一个地进行 6 个连续调用,那么打开一个并重复使用可能是有意义的。但除此之外,我想说的是坚持你正在做的事情。
您可能想要研究的唯一东西是连接管理器,这样您就不必一遍又一遍地在 .net 中创建连接对象。但这与数据库连接和对象创建没有任何关系。
.Net has connection pooling already managed for you so you don't need to worry about reusing connections like you might have with old asp. I always go with several small quick calls vs keeping one open the whole time because not all the time it's open is generally used for calls. You have your site code running doing some things between as well.
Now if you are going to make 6 consecutive calls one after another then it might make sense to open one and reuse. But other than that I say just stick with what you are doing.
The only thing you might want to look into is a connection manager so that you aren't having to create the connection object in .net over and over again. But that doesn't have anything to do with db connections vs just object creation.
您应该尽可能短地保持连接打开。因此,您想要打开连接,执行查询或存储过程,然后关闭连接。尽管这听起来很昂贵,但它利用了 ADO.NET 的内置连接池。当您关闭连接时,它将返回到池中并重新使用,因此您不会遭受性能影响。
You should keep connections open as short a time as possible. Thus, you want to open a connection, execute a query or stored procedure and then close the connection. Although this sounds expensive, it leverages ADO.NET's built-in connection pooling. When you close a connection, it is returned to a pool and reused so you do not suffer a performance hit.
ADO.Net 使用连接池,因此可以降低打开新连接的成本,从而避免在整个应用程序中打开一个连接的需要。
但是,重新整理池中的连接可能仍然存在一些开销,因此,如果您的代码在代码的同一部分内按顺序立即运行,那么您最好使用如此短的跨度的单个连接。检查 Adam 的答案以了解更多信息 - 您希望首先设置其他所有内容,以便连接打开的时间尽可能短。
如果任何 ADO.Net 程序员可以确认或更正此问题,请这样做。
ADO.Net uses connection pooling, so that should lower the cost of opening new connections avoiding any need to have one connection open throughout your whole application.
However, there is probably still some overhead to shuffling the connections in the pool, so if you have code that is run sequentially and immediately, within the same portion of your code, then you are probably better off using a single connection for that short span. Check Adam's answer for a bit more on that - you want to get everything else set up first so that the connection is open for as short a time as possible.
If any ADO.Net programmers can confirm or correct this, please do.
更好的方法是准备这两个命令,然后打开连接并快速连续执行它们:
始终保持它们打开的时间尽可能短。
A better way to do this would be to prep both of the commands and then open the connection and execute them both in quick succession:
Always keep them open for as short a time as possible.