我应该在哪里放置数据库连接字符串以及如何处理连接池?

发布于 2024-11-05 09:13:15 字数 476 浏览 6 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(7

番薯 2024-11-12 09:13:15

您可能没有正确关闭打开的连接。

增加“水池大小”就像在瀑布下放一个更大的水桶一样——会有帮助,但效果不大。

尝试找到发生类似情况的区域:

con.Open();

确保如果它不在 try/catch 中,则它在一个 try/catch 中,并且它包含一个 finally 语句。

try {
   con.Open();
   //several other data base releated
   //activities
} catch (Exception ex) {
  // do something
} finally {
  con.Close();
}

另外,为了避免使用 finally 块,您可以将 SqlConnection 包装在 using 语句中。

  using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["yourKey"].ConnectionString))
        {

            // write your code here 

        }

关于您关于连接字符串的问题,是的,将其存储在您的 web.config 中

<connectionStrings>
<add name="name" connectionString="Data Source=;Initial Catalog=;User ID=sa;password=;Persist Security Info=True;Connection TimeOut=20; Pooling=true;Max Pool Size=500;Min Pool Size=1" providerName="System.Data.SqlClient"/>
</connectionStrings>

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:

con.Open();

Ensure that if it's not in a try/catch, that it is in one, and that it includes a finally statement.

try {
   con.Open();
   //several other data base releated
   //activities
} catch (Exception ex) {
  // do something
} finally {
  con.Close();
}

Also, to avoid having to use the finally block, you can just wrap the SqlConnection in a using statement.

  using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["yourKey"].ConnectionString))
        {

            // write your code here 

        }

In regards to your question about connection string, yes store it in your web.config

<connectionStrings>
<add name="name" connectionString="Data Source=;Initial Catalog=;User ID=sa;password=;Persist Security Info=True;Connection TimeOut=20; Pooling=true;Max Pool Size=500;Min Pool Size=1" providerName="System.Data.SqlClient"/>
</connectionStrings>
凝望流年 2024-11-12 09:13:15

将其存储在 web.config 文件的 connectionStrings 部分中:

<connectionStrings>
    <add name="name"
        connectionString="Data Source=;Initial Catalog=;User ID=sa;password=;Persist Security Info=True;Connection TimeOut=20; Pooling=true;Max Pool Size=500;Min Pool Size=1"
        providerName="System.Data.SqlClient"/>
</connectionStrings>

然后您将能够在代码中访问它...

ConfigurationManager.ConnectionStrings["name"].ConnectionString

Store it in the web.config file, in the connectionStrings section:

<connectionStrings>
    <add name="name"
        connectionString="Data Source=;Initial Catalog=;User ID=sa;password=;Persist Security Info=True;Connection TimeOut=20; Pooling=true;Max Pool Size=500;Min Pool Size=1"
        providerName="System.Data.SqlClient"/>
</connectionStrings>

And then you will be able to access this in your code...

ConfigurationManager.ConnectionStrings["name"].ConnectionString
眼眸 2024-11-12 09:13:15

你也可以使用 using ,

如果你使用“using”,它会自动处置对象,

不需要 con.close 和所有

  using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["yourKey"].ConnectionString))
        {

     // write your code here 

        }

you can do using also

it will automatically disposes the object

if you use "using" there is no need of con.close and all

  using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["yourKey"].ConnectionString))
        {

     // write your code here 

        }
葮薆情 2024-11-12 09:13:15

将连接字符串存储在 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

甜宝宝 2024-11-12 09:13:15

使用两者的帮助

try
{
 con.Open();
}
catch(Exception ex)
{
if(con.State== ConnectionState.Open)
con.Close();
}
finally
{
con.Close();
}

,并在 Web.Config 中的“配置”下添加连接字符串。这会对你有所帮助。

Use the Help of Both

try
{
 con.Open();
}
catch(Exception ex)
{
if(con.State== ConnectionState.Open)
con.Close();
}
finally
{
con.Close();
}

and also add the Connection String in Web.Config, under Configuration. This will help you.

╰沐子 2024-11-12 09:13:15

是的,将其存储在 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.

离不开的别离 2024-11-12 09:13:15

最好的选择是使用键入的设置。

打开您的项目属性。
转到“设置”选项卡。
添加新设置,例如MainConnectionString,选择设置类型(ConnectionString)。在值中插入连接字符串或点击“...”按钮以打开一个对话框来构建连接字符串。

现在您可以在代码中引用您的连接字符串,如下所示:

Settings.Default.MainConnectionString

如果您打开配置文件,您将看到

  <configuration>
    <connectionStrings>
      <add name="WebApplication1.Properties.Settings.MainConnectionString"
           connectionString="Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True"
           providerName="System.Data.SqlClient" />
    </connectionStrings>

您可以拥有此连接字符串
指定:

  • 针对其自己的 web.config 中的一个网站。
  • 对于一组网站
  • 对于一个盒子上的所有网站:在机器范围 web.config 中。
  • 对于盒子上的所有应用程序:在 machine.config 中。

如果您有许多应用程序连接到同一数据库并安装在一台机器上,这会很方便。如果数据库位置发生更改,您只需更新一个文件 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

  <configuration>
    <connectionStrings>
      <add name="WebApplication1.Properties.Settings.MainConnectionString"
           connectionString="Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True"
           providerName="System.Data.SqlClient" />
    </connectionStrings>

You can have this connection string
specified:

  • for one web site in its own web.config.
  • for a group of web sites
  • for all web sites on a box: in machine scope web.config.
  • for all application on a box: in the machine.config.

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.

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