Click here to Skip to main content
15,896,541 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

How do I declare multiple connections in a .aspx webform and call them in seperate web forms when needed?
Posted
Comments
Dave Kreskowiak 13-May-15 22:48pm    
You might want to explain what you're doing and why you think you need "multiple connections" and explain connections to what?

Your question as it stands is meaningless and unanswerable.

Hello
Good Day !!!
Insted of Multi connection string you can use dynamic connection string this will help you...

from the below links you can get idea about multi connection strings

Dynamically create connectionString in app.config using C#[^]


Configuring a Connection String in the App.Config File During Runtime in C#[^]


Dynamic Connection String[^]


Dynamically access a database through a Web Application[^]
 
Share this answer
 
v2
If the connections are all for the same database, same credentials, then there is no point in doing this, simply create the connection as and when you need it, then use it. If the connections require difference connection strings then create an entry in the web.config for each string

XML
<configuration>
  <connectionStrings>
    <add name="Conn1" connectionString="server=a;database=b;Integrated Security=true"/>
    <add name="Conn2" connectionString="server=c;database=d;username=blah;password=mypassword;"/>
  </connectionStrings>


Then read them via ConfigurationManager when you need them

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn1"].ConnectionString);


If you're wanting to create SqlConnection objects and kept them between page requests etc then don't do this, it is bad for performance and resource use, let ado.net handle the connections and connection pooling for you by simply creating connections when you need them. Google "ado.net connection pooling" if you want to learn more.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900