设计者生成的表适配器如何处理连接
表适配器如何使用连接?
稍微解释一下,它们是否会自动打开和关闭连接,或者如果我在调用表适配器方法之前已经打开连接,它们是否会使用它并使其保持打开状态?
问候
How do table adapters make use of connections?
To explain that a bit, do they automatically open and shut connections or if I already have the connection open before calling a tableadapter method, do they use it and leave it open?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您查看设计器生成的代码,您会发现如果存在连接,适配器会重用它,否则会创建一个新连接。 执行查询方法时,如果连接未打开,该方法将打开它。 如果该方法打开了它,它会在完成后关闭它。 默认情况下,您会为每个表适配器获得一个新连接。
If you look at the designer-generated code, you'll see that if there is a connection, the adapter reuses it, otherwise it creates a new one. When executing a query method, if the connection isn't open, the method opens it. If the method opened it, it closes it when it is done. By default you get a new connection for every table adapter.
以下是设计者生成的典型表适配器函数的代码:
根据 MSDN,
DbDataAdapter.Fill
的行为如下:参考:填充方法(数据表)
但是,在设计器生成的插入/删除/更新,代码如下所示:
Here is the code of a typical designer-generated Table Adapter's function:
According to MSDN,
DbDataAdapter.Fill
behave like this:Ref: Fill Method (DataTable)
However, in a designer-generated Insert/Delete/Update, the code would look like this:
是的,如果您之前打开连接并传递给适配器,则表适配器将保持连接打开,如果您传递关闭的连接,则打开并关闭适配器,或者保留默认连接
yea, tableadapter is leave the connection open if you opened it before and pass to adapter, and open and closa adapter if you pass closed connection, or leave the default conenction
你可以这样做:
或者
至于连接寿命,如果没有打开,它会为你打开和关闭它。
You can do it like this:
or
As for the connection lifespan, if it is not opened, it will open and close it for you.
时会发生什么
当连接保持打开状态 ? 或者它关闭了?
What happens when
Did the connection leave open? or it closed?