c#如何实现线程查询Data Base

发布于 2024-10-02 13:05:17 字数 963 浏览 3 评论 0原文

你好,我有一种情况,实体是 customerManager 仓库客户和供应商 我的目标是:

  1. 仓库是单例的,并在运行时打开数据库。
  2. customerManager 将客户管理为查询仓库并更新仓库的线程(在购买员工之后)。
  3. 当仓库中的一件商品用完时,我们要求不同线程中的供应商为我们提供它,而供应商做他的事情(假设大约 5 秒),客户等待(在队列中)并在以下情况下调用供应商方法返回true(让我们假设它总是返回true)..

所以我的问题是关于三件事:

  1. 设计 - customerManager是否应该在他内部保存仓库和客户?这似乎是最好的灵魂,有人推荐吗?(c# 设计主题)

  2. 一次可以有多少个线程访问数据库?数据库可以自己处理它,这样我就不需要自己做吗?我应该为他们保存 SqlCommand(s) 吗?我应该使用数据集还是数据读取器?换句话说有人可以建议我该怎么做吗? 我应该为 10 个线程做什么:

     for (int i = 0 ; i < 10 ; i++)
    {
        SqlConnection sqlConnection = new SqlConnection(r_ConnectionString);      
        sqlConnection.Open();
        sqlConnection.Close();
    }
    

...所以连接池将为 10 个连接打开?

**数据库ADO.NET **主题

  1. 线程应该如何在队列中等待?(以便等待供应商方法唤醒它们)如何唤醒它们? c# 中有一个好的解决方案吗? (c# 线程主题)

我认为这个问题太长了,否则就太断章取义了,所以如果您能在标题中写下您想要引用的问题,我将不胜感激。

谢谢 。

hello there i have a situation the entities are customerManager warehouse customer and suppliers
my goals are :

  1. the warehouse is singletone and open db in runtime.
  2. the customerManager manage customers as threads who query the warehouse and update it (after buying staff).
  3. when one of the items in the warehouse is run out of we ask supplier in a diffrent thread to supply it for us ' while the supplier does his thing (let's assume it's something like 5 seconds ) the customer waits(in queue) and invoked when the supplier method returned true (let's assume it return true always)..

so my questions are about 3 things :

  1. design - should the customerManager holds inside him the warehouse and the customers ? it seems like the best soultion, does someone recommend otherwise?(c# design topic )

  2. how many threads can go to the db at once ? can a db handle it by himself so i wont need to do it myself ? should I hold for them SqlCommand(s)? should I use dataset or datareader? in other words can someone advice me how to do it ?
    should i do for 10 threads :

     for (int i = 0 ; i < 10 ; i++)
    {
        SqlConnection sqlConnection = new SqlConnection(r_ConnectionString);      
        sqlConnection.Open();
        sqlConnection.Close();
    }
    

...so the conection pool would be open for 10 connectiones ?

** database ADO.NET ** topic

  1. how should the threads wait in a queue?(in order to wait to the supplier method to awake them ) how to wake them ? is there a good solution in c# for that ? (c# threads topic)

I think the question is too long but otherwise would be too out of context so I would appreciate if you would write in the the title what question you want to reference.

thank you .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-10-09 13:05:17

您的工作线程可以通过 BlockingCollectionConcurrentQueue

对于连接管理,您最好这样做:

using (SqlConnection conn = new SqlConnection(...))
{
}

因为这可以确保为您调用 Dispose() 。正如其他反馈中所述,您可以执行此操作,而不必担心数据库的实际连接计数,因为 ADO.Net 在幕后管理物理连接池

没有人可以告诉您 DataSetDataReader 哪个效果最好,这取决于加载数据后您对数据的使用情况。 DataReader 依次提供每条记录的顺序读取,而 DataSet 提供底层数据库数据的内存缓存,从这个意义上来说,它是“更高级别”的抽象。

Your worker threads could be fed work via BlockingCollection or ConcurrentQueue.

For connection management you are better off doing this:

using (SqlConnection conn = new SqlConnection(...))
{
}

since this ensures Dispose() gets called for you. As noted in other feedback, you can do this without worrying about actual conn count to the DB since ADO.Net manages a pool of physical connections behind the scenes.

Nobody can tell you whether DataSet or DataReader works best, it depends on your usage of the data once it's loaded. DataReader provides a sequential read of each record in turn, while DataSet provides an in-memory cache of underlying DB data and in that sense is a 'higher-level' abstraction.

暗藏城府 2024-10-09 13:05:17

SqlConnections 在.net 中使用连接池实现。您无需担心连接本身的管理。对您的唯一要求是在打开它们后调用 .close。 .net 将以高效的方式为您管理剩下的事情。

如果您想同时运行多个查询,则可以使用 begin invoke 和 end invoke 调用 sql 命令。

通过使用这两种方法,您可以在不需要管理线程的同时获得多线程行为的水平上工作。

然而,您应该阅读 ADO.Net,因为当您了解它的工作原理时,您所讨论的很多内容都是不必要的。

至于数据集或数据读取器,这取决于您的问题,尽管数据集是一个非常重的对象,但数据读取器是轻量级且快速的,可以让您相当轻松地填充集合。

我更喜欢使用 linq2sql 或实体框架。 ADO.Net 有点脆弱,因为您必须对数据进行大量转换并手动映射到在运行时而不是编译时容易出错的对象。

SqlConnections are implemented in .net using a connection pool. You do not need to worry about the management of the connections themselves. The only requirement on you is that adfter you open them you call .close. .net will manage the rest for you in an efficient way.

If you wanted to run multiple queries simultaneously then you can call the sqlcommand wtih begin invoke and end invoke.

By using both of these you cna work at a level that does not require you to mange the threads whilst gettting a multi threaded behaviour.

However you shuold read up on ADO.Net because a lot of what you are talking about is unnecessary when you kow how it works.

as for dataset or datareader that depends on your problem, Dataset is a very heavy object though, datareaders are lightweight and fast that allow you to populate a collection fairly easily.

I prefer using linq2sql though or entity framework. ADO.Net is kinda fragile because you ahve to do a lot of casting on data adn manual mapping onto objects that is prone to errors at run time rather than compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文