我应该在哪里放置数据库连接字符串以及如何处理连接池?
我正在开发一个 ASP.NET 应用程序,该应用程序托管在 IIS 服务器上。要打开连接,我使用:
SqlConnection con = new SqlConnection("Server = INLD50045747A\\SQLEXPRESS;
Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;");
con.Open();
是否可以将此连接字符串存储在某处,以便我不需要在每个 aspx.cs 文件中写入?我正在使用 MSSQL 数据库。
我面临的一个问题是:
从池中获取连接之前超时时间已过。发生这种情况的原因可能是所有池连接都在使用中并且已达到最大池大小
我在某处读到要求我将最大连接池增加到 100。这会影响我的应用程序的性能吗?
I am developing an asp.net application which I have hosted on an IIS server. To open a connection I use:
SqlConnection con = new SqlConnection("Server = INLD50045747A\\SQLEXPRESS;
Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;");
con.Open();
Is it possible to store this connection string somewhere so that I don't need to write in every aspx.cs file? I am using MSSQL database.
I'm facing an issue which says:
The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached
I read somewhere which asks me to increase the maximum connection pool to 100. Will it affect the performance of my application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能没有正确关闭打开的连接。
增加“水池大小”就像在瀑布下放一个更大的水桶一样——会有帮助,但效果不大。
尝试找到发生类似情况的区域:
确保如果它不在 try/catch 中,则它在一个 try/catch 中,并且它包含一个 finally 语句。
另外,为了避免使用
finally
块,您可以将 SqlConnection 包装在 using 语句中。关于您关于连接字符串的问题,是的,将其存储在您的 web.config 中
You probably aren't closing your open connections propertly.
Increasing the "pool size" is like putting a bigger bucket under a waterfall - it will help, but barely.
Try and locate areas where something like this is happening:
Ensure that if it's not in a try/catch, that it is in one, and that it includes a finally statement.
Also, to avoid having to use the
finally
block, you can just wrap the SqlConnection in a using statement.In regards to your question about connection string, yes store it in your web.config
将其存储在
web.config
文件的connectionStrings
部分中:然后您将能够在代码中访问它...
Store it in the
web.config
file, in theconnectionStrings
section:And then you will be able to access this in your code...
你也可以使用 using ,
如果你使用“using”,它会自动处置对象,
不需要 con.close 和所有
you can do using also
it will automatically disposes the object
if you use "using" there is no need of con.close and all
将连接字符串存储在 web.config 文件中。你可以找到很多例子。检查此属性。 http:// /msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.71%29.aspx
谢谢
香卡
Store the connection string in the web.config files. you can find numerous examples. Check this for the properties. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.71%29.aspx
Thanks
Shankar
使用两者的帮助
,并在 Web.Config 中的“配置”下添加连接字符串。这会对你有所帮助。
Use the Help of Both
and also add the Connection String in Web.Config, under Configuration. This will help you.
是的,将其存储在 web.config 文件中,但请确保如果出现错误,它不会向用户显示 web.config 文件的内容(从而向全世界显示您的密码。)
如果您使用相同的连接在许多应用程序中,您可以考虑编写一个服务来提供连接字符串,这样您只需在一处更改它们。
Yes, store it in the web.config file but make sure that if there is an error it doesn't display the content of the web.config file to the user (thus showing the world your password.)
If you use that same connection string in a lot of applications you could consider writing a service to provide the connection strings, that way you only have to change them in one place.
最好的选择是使用键入的设置。
打开您的项目属性。
转到“设置”选项卡。
添加新设置,例如
MainConnectionString
,选择设置类型(ConnectionString)
。在值中插入连接字符串或点击“...”按钮以打开一个对话框来构建连接字符串。现在您可以在代码中引用您的连接字符串,如下所示:
Settings.Default.MainConnectionString
如果您打开配置文件,您将看到
您可以拥有此连接字符串
指定:
如果您有许多应用程序连接到同一数据库并安装在一台机器上,这会很方便。如果数据库位置发生更改,您只需更新一个文件 machine.config,而不是转到每个应用程序的配置文件。
The best option is to use typed settings.
Open your project properties.
Go to Settings tab.
Add new setting, for example
MainConnectionString
, select setting type(ConnectionString)
. In the value insert your connection string or hit '...' button to bring a dialog to build connection string.Now you can reference your connection string in the code like this:
Settings.Default.MainConnectionString
If you open your config file you will see
You can have this connection string
specified:
This can be convenient if you have a lot of applications that connect to the same db and are installed on one box. If db location changes you update just one file machine.config, instead of going to each application's config file.