使数据库连接全局化 [英] Making a database connection global

查看:59
本文介绍了使数据库连接全局化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络解决方案


目标:拥有全球数据库连接

为什么:(将有30多个表,由30多个班级代表)我只想要

一次引用数据库连接。


我把连接字符串放在web.config中。

我创建了一个具有静态数据库连接的类并且该类打开并且

关闭数据库。


这似乎工作所以问题:是否有更好的方法来处理

数据库连接?


在此先感谢。

解决方案

这个静态数据库连接对象在整个应用程序的整个生命周期内保持打开状态?通常不建议长时间保持

数据库连接处于打开状态。通常,你想要

来打开连接,执行一个命令,并立即关闭连接




" Terry乔利" < TE *** @ ipmas.com>在消息中写道

news:44 ********************** @ news.twtelecom.net ..。

网络解决方案

目标:拥有全球数据库连接
为什么:(将有30多个表,由30多个班级代表)我只想参考数据库连接一次。

我把连接字符串放在web.config中。
我创建了一个带有静态数据库连接的类,该类打开
并关闭数据库。 br />
这似乎有用,所以问题是:有没有更好的方法来处理数据库连接?

在此先感谢。



" Terry Jolly" < TE *** @ ipmas.com>在留言中写道

news:44 ********************** @ news.twtelecom.net ..
< blockquote class =post_quotes>网络解决方案

目标:拥有全球数据库连接
为什么:(将有30多个表,由30多个类代表)我只想
引用数据库连接一次。

我把连接字符串放在web.config中。
我创建了一个带有静态数据库连接的类,该类打开
并关闭数据库。

这似乎工作所以问题:有没有更好的方法来处理
数据库连接?




是的 - 几乎所有其他东西都比你提出的要好。


如果你这样做,你会很快遇到问题,因为每个用户都是这样的。 $ b请求页面将共享相同的连接。这意味着一次只有一个用户可以请求一个页面,或者你会收到一条错误信息

因为连接繁忙。每个使用数据库的页面都需要其自己的连接。这是你在ASP.NET中可以做的最糟糕的事情之一 -

你应该做的最后一件事是保持与你的RDBMS的连接打开

超过必要的,更不用说你的

网络应用程序的整个生命周期。


考虑使用DAL(数据访问层)代替:
http://www.15seconds.com/issue/030317.htm


这是设计问题;)


我的建议是有一个只处理命令的类

数据库。


你的其他30多个课程只准备命令并传递给他们执行

。所以你将拥有工作的中心位置

SQLConnection对象


希望这会有所帮助


Galin Iliev [MCSD .NET]
www.galcho.com


Web Solution

Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.

I put the connection string in the web.config.
I created a class with a static database connection and the class opens and
closes the database.

This seems to work so the question: Is there a better way to handle the
database connection?

Thanks In Advance.

解决方案

Does this static database connection object stay open throughout the entire
lifetime of the application? It is typically not recommended to keep a
database connection open for an extended period of time. Usually, you want
to open the connection, execute a command, and close the connection
immediately.

"Terry Jolly" <te***@ipmas.com> wrote in message
news:44**********************@news.twtelecom.net.. .

Web Solution

Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.

I put the connection string in the web.config.
I created a class with a static database connection and the class opens
and closes the database.

This seems to work so the question: Is there a better way to handle the
database connection?

Thanks In Advance.



"Terry Jolly" <te***@ipmas.com> wrote in message
news:44**********************@news.twtelecom.net.. .

Web Solution

Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.

I put the connection string in the web.config.
I created a class with a static database connection and the class opens
and closes the database.

This seems to work so the question: Is there a better way to handle the
database connection?



Yes - pretty much anything else would be better than what you''re proposing.

If you did this you would quickly run into problems, as every user that
requests a page will be sharing the same connection. That means that only
one user at a time can request a page, or you will get an error message
because the connection is busy. Every page that uses the database needs its
own connection. This is one of the worst things you can do in ASP.NET - the
absolute LAST thing you should do is to keep a connection to your RDBMS open
for any longer than is necessary, let alone for the entire lifetime of your
web app.

Consider using a DAL (Data Access Layer) instead:
http://www.15seconds.com/issue/030317.htm


well this is design question ;)

my suggestion is having one class that just handles commands to
database.

your other 30+ classes would just prepare commands and pass them for
execution. So you will have central place for working with
SQLConnection object

Hope this helps

Galin Iliev[MCSD.NET]
www.galcho.com


这篇关于使数据库连接全局化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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