一个关于程序设计的问题
我正在开发一个 Windows Mobile WinForm 应用程序,该应用程序使用 Sql Server CE 3.1 以及 .NET Compact Framework 2.0 SP2 和 C#。
我有一个包含 SqlCeConnection 对象的表单,该对象在其所有执行时间内打开:我在启动时打开连接,并在事件关闭时关闭它。
我还有一个类来读取Sql Server CE数据库。
我的问题是关于性能的:这两种情况中哪一种是最好的?
1.当我创建 reader 类的对象时,将 SqlCeConnection 对象传递给构造函数并将其保存到这个新对象作为属性。
2. 当我调用此读取器类的方法时,始终将 SqlCeConnection 对象作为参数传递。
我想如果我使用情况1,我有两个SqlCeConnection对象,不是吗?
如果您需要更多详细信息,请告诉我。
谢谢你!
I'm developing a Windows Mobile WinForm application that uses Sql Server CE 3.1 with .NET Compact Framework 2.0 SP2 and C#.
I have a form that has a SqlCeConnection object, opened during all of it's execution time: I open the connection at startup and close it on event closing.
I also have a class to read the Sql Server CE database.
My question is about performance: Which of these two situations is the best?
1. When I create a object of reader class, pass to contructor the SqlCeConnection object and hold it into this new object as a property.
2. Always, when I call a method of this reader class, pass the SqlCeConnection object as a parameter.
I think if I use situations 1, I have two SqlCeConnection objects, isn't it?
If you need more details, tell me.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先回答您的最后一个问题,在场景 1 中您不会有两个不同的连接。您传入的 SqlCeConnection 是一个引用,因此表单和阅读器类都具有对同一基础连接对象的引用。
实际上我不认为这两种情况有很大的差异。由于您在表单的生命周期内保持连接打开,因此是否在该辅助类的构造函数或每个方法中传递它实际上并不重要。所以我想场景 1 会更容易,这样每次调用的参数列表都会更小。
如果您想尽量缩短连接的生命周期,这一点就很重要。例如,在连接到普通 SQL Server 的 Windows 桌面应用程序中,您实际上并不希望在表单的生命周期内保持打开的连接,因为如果很多人打开大量表单,则会出现性能问题你的服务器。因此,在这种情况下,您将在最后一秒创建连接,将其传递到读取器例程以获取数据并立即关闭它。在这种情况下,场景 2 更适合您。
但在移动应用程序中,这种情况对您来说似乎并不重要。您可能是唯一连接到数据库的人,因此即使保持连接打开也没关系。
To answer your last question first, no you won't have two different connections in scenario 1. The SqlCeConnection that you pass in is a reference, so the form and the reader class both have a reference to the same underlying connection object.
I actually don't think there's a huge amount of difference in the two scenarios. Since you're keeping the connection open for the lifetime of the form, it really doesn't matter if you pass it in the constructor of this secondary class or for each method. So I guess it's easier just to do scenario 1 that way the parameter list for each call is smaller.
This would matter if you were trying to minimize the lifetime of your connection. In, for instance, a Windows Desktop app connecting to a normal SQL Server, you wouldn't really want to hold open connections for the lifetime of the form because if lots of people were opening lots of forms, you've got performance problems on your server. So in that case, you would create the connection at the last second, pass it into your reader routine to get the data and immediately close it. Scenario 2 would be better for you in this case.
But that scenario doesn't seem important to you in a mobile application. You're probably the only one connecting to the database so it doesn't matter if you hold the connection open.