最好的锻炼?打开和关闭多连接,或ado.net一个大的开放连接 [英] Best practice? open and close multi connections, or one large open connection for ado.net

查看:144
本文介绍了最好的锻炼?打开和关闭多连接,或ado.net一个大的开放连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ADO.Net一些数据库连接,我只是希望我做了正确的方式。我打开和关闭的多个连接每个存储过程。或者我应该可以(在数据库中占用资源少,也许?)这个包裹在短短一个打开的连接的感谢,如果有什么奇怪什么我可以做的更好,让我知道,谢谢!

示例:这一点,但我有一个6人......

 使用(SqlConnection的康恩=新的SqlConnection(ConnectionString的))
        {
            的SqlCommand CMD =新的SqlCommand(spSelectAllTrip,康涅狄格州);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            ddlTripTypeA.DataSource = cmd.ExecuteReader();
            ddlTripTypeA.DataTextField =TRIPTYPE;
            ddlTripTypeA.DataValueField =TripTypeAID;
            ddlTripTypeA.DataBind();
        }
        使用(SqlConnection的康恩=新的SqlConnection(ConnectionString的))
        {
            的SqlCommand CMD =新的SqlCommand(spSelectAllTripB,康涅狄格州);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            ddlTripTypeB.DataSource = cmd.ExecuteReader();
            ddlTripTypeB.DataTextField =TRIPTYPE;
            ddlTripTypeB.DataValueField =TripTypeBID;
            ddlTripTypeB.DataBind();
        }
 

解决方案

净对你已经成功,所以你不必担心重用像你这样的连接可能会与旧的ASP连接池。我经常去的几个小灵通通话VS守信开全时间,因为不是所有的时间它是开放的,通常用于通话。你有你的网站code运行做之间还有一些事情。

现在,如果你要作连续6的呼声此起彼伏,那么它可能是有意义的开立一个和重用。但除此之外,我说的只是坚持自己在做什么。

你可能要考虑的唯一事情是一个连接管理器,使您不必在创建.NET连接对象一遍又一遍。但是,这并没有什么关系数据库连接VS刚刚创建对象。

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();
        }

解决方案

.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一个大的开放连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆