Where should I place my database connection string and how do I handle connection pooling?

我的梦境 提交于 2019-12-01 11:09:47

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>
Muhammad Akhtar

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

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 

        }

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!