几个问题的最佳做法... [英] Best practices on a couple of issues...

查看:100
本文介绍了几个问题的最佳做法...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在VB.Net中做ASP.Net页面方面有相当的经验.我现在正在使用C#WinForms应用程序.此应用程序使用SQLite数据库进行数据存储.当我坐在这里进行编程时,我想到打开与数据库的连接,并在应用程序使用期间使其保持打开状态,并使所有类都可以使用它,这是一件简单的事情.在进行Web开发时,这是不切实际的,因为它是无状态的",对于大多数事情,您需要回发,然后才能知道是否对数据库进行了任何操作.因此,我想我的问题是,为整个winform应用程序使用1个连接(使用每种类型的命令来完成工作)是否是一个好主意?那有道理吗? WinForm开发人员是否总是在每种方法中都包含一个新的连接?例如

Ok, so I have a fair bit of experience in doing ASP.Net pages in VB.Net. I am now working on a C# WinForms app. This app uses a SQLite DB for data storage. As I sit here programming it occurs to me that it would be a simple matter to open a connection to the database and leave it open for the duration of the application and make it available to all the classes to use. When doing web development this isn''t practical because it is "stateless" and for most things you need a postback before you know whether or not you are doing anything with the database. So, I guess my question is, is it a good idea to have 1 connection for the whole winform app to use (with commands in each type to do the work)? If that makes sense? Do you winForm developers alway enclose a new connection with each method? e.g.

class myclass1
{
  public void doSomething
{
  SQLiteConnection conn = new SQLiteConnection(myString);
  conn.Open();
  SQLiteCommand cmd = new SQLiteCommand("someSqlStatement", conn);
  SQLiteDataReader reader = cmd.ExecuteReader();
  //loop and do work.
  conn.close()
}
}

class myclass2
{
  public void doSomethingElse
{//get another connection again
  SQLiteConnection conn = new SQLiteConnection(myString);
  conn.Open();
  SQLiteCommand cmd = new SQLiteCommand("someSqlStatement", conn);
  SQLiteDataReader reader = cmd.ExecuteReader();
  //loop and do work.
  conn.close()
}
}



或者,您可以将连接传递给其构造函数中的每个对象,然后直接引用它:



Or alternatively you could pass the connection to each object in its constructor and then reference it directly:

class myclass3
{
  public void doSomethingEasy
{//use "globalConn" which was passed in the constructor
  SQLiteCommand cmd = new SQLiteCommand("someSqlStatement", globalConn);
  SQLiteDataReader reader = cmd.ExecuteReader();
  //loop and do work.
  conn.close()
}
}


由于我的另一个问题与这个问题无关,因此我将其单独发布.

像往常一样谢谢!


Since my other question is pretty unrelated to this one, I''ll post it separately.

Thanks as always!

推荐答案

我没有对那些不是Web应用程序的数据库做任何事情,所以我可能会错,但我会精益一个单一的连接.正如您所说明的,当您不必担心在每个方法中创建和销毁连接时,代码会简单得多(尽管可以说,即使在Web应用程序中也不必这样做). >
但是,您可能会遇到的一个问题是,如果您有多个并发用户,并且始终保持连接处于打开状态,则数据库侧的连接可能用光了!

尽管在您的示例中我看到您正在关闭全局连接,但没有打开它-这将行不通! ;)
I haven''t done anything with databases that wasn''t a web app, so I could be wrong, but I''d lean toward having a single connection. As you illustrated, the code is much simpler when you don''t have to worry about creating and destroying connections in every single method (though arguably you shouldn''t necessarily have to do that even in a web application).

One issue you might run into, though, is if you have numerous concurrent users and you keep your connection open all the time, you might run out of connections on the database side!

Though I see in your example that you are closing the global connection, but not opening it - that won''t work! ;)


这篇关于几个问题的最佳做法...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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