通过服务使SQLConnection可见 [英] Make SQLConnection visible through out the Service

查看:104
本文介绍了通过服务使SQLConnection可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我有一个Web服务,其中有3个vb.net模块

1. ApplicationManager.vb

2. AgencyManager.vb

3. InventoryManager.vb

和我的服务InventoryService.asmx,在其中我在ApplicationManager.vb中调用GeAllAgencies,然后该方法在AgencyManager.vb中调用GetAgency

并且该GetAgency函数调用InventoryManager.vb中的方法,例如GetAllItems,GetAvailableItems ...,等等,还有更多方法.

我的问题是,我每次在每个模块中每次都打开SQLConnection()时,例如ApplicationManager中的GetAgency()函数,AgencyManager中的GetAgency()函数,最后InventoryManager中的所有方法为每个人打开一个连接,这导致我打开很多连接和问题是,当我第一次运行我的服务时,我很快就获取了数据,但是第二次(我是说在同一运行中,我调用了第一项服务,而第二次调用了),我无法获取数据.我认为此问题是由于更多的连接而发生的,而我的问题是

如何打开sqlconnection(我想在InventoryService.asmx中打开),以便整个应用程序都可以看到它,并且使用相同的con对象.

可以使整个应用程序都可见的单个"SQLConnection",并将连接字符串保存在要通过以下代码访问的web.config文件中吗?

昏暗的构造为字符串= ConfigurationManager.ConnectionStrings("connstr").ConnectionString

请告诉我该怎么做?

在此先感谢
Ganesh

Hi friends, I have one web service in which I have 3 vb.net modules

1. ApplicationManager.vb

2. AgencyManager.vb

3. InventoryManager.vb

and my service InventoryService.asmx in which I call the GeAllAgencies in the ApplicationManager.vb and in turn that method calls the GetAgency in AgencyManager.vb

and that GetAgency function calls the methods in InventoryManager.vb like GetAllItems,GetAvailableItems ... so on there are more methods.

My problem is that I am opening SQLConnection() every time in every module like in GetAgency() function in ApplicationManager, GetAgency() function in AgencyManager and at last all the methods in InventoryManager opens a connection for each individual, this cause me to open a lot of connections and problem is that when I run my service for the first time I am getting the data fastly but for the second time (I mean in the same run I invoke first service and in the second service invoke) I am unable to get the data. I thought this problem is occurred due to more connections and my question is

how to open a sqlconnection (I want to open in InventoryService.asmx) so it is visible to whole application and with the same con object.

can I make the single "SQLConnection" that is visible to whole appliation and I saved my connection string in web.config file from which I am accessing through with the following code

Dim constr As String = ConfigurationManager.ConnectionStrings("connstr").ConnectionString

Please tell me How to do this?

Thanks in Advance
Ganesh

推荐答案

通常,规则是尽可能晚地打开SQL连接并尽快关闭.最好只打开/关闭数据访问层的连接,即直接访问数据的类,不要在其他地方打开/关闭连接.

您可以做的是创建一个单例连接实例,即在一个可以在整个数据访问层中重用的类中,这样您就不必重复打开/关闭.一次打开...只要您想一直使用它,...最后,在完成操作后将其关闭(即使您不关闭它,它也将在默认时间后超时).

可以打开更多的想法.
Generally, the rule is to open the SQL connection as late as possible and close as quickly as possible. Preferably you should open/close connections only the Data access layer i.e. the class(es) that directly access the data and no where else.

What you can do is create a singleton connection instance i.e. in a class that can be reused throughout the data access layer so that you don''t have to open/close repeatedly. Open once ...keep using it as long as you want to ...finally, close it when you are all done (even if you don''t close it, it will timeout after the default time anyway).

Open to more ideas though.


这篇关于通过服务使SQLConnection可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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