如何在网页中声明多个连接并在需要时调用它们 [英] How to declare multiple connections in a webpage and call them when needed

查看:59
本文介绍了如何在网页中声明多个连接并在需要时调用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在.aspx webform中声明多个连接,并在需要时以单独的Web表单调用它们?

Hi,

How do I declare multiple connections in a .aspx webform and call them in seperate web forms when needed?

推荐答案

你好

美好的一天!!!

多连接字符串,你可以使用动态连接字符串,这将帮助你...



来自以下链接你可以了解多连接字符串



使用C#在app.config中动态创建connectionString [ ^ ]





配置C#中运行时期间App.Config文件中的连接字符串 [ ^ ]





动态连接字符串 [ ^ ]

< br $> b $ b

通过Web应用程序动态访问数据库 [ ^ ]
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[^]


如果连接都是针对同一个数据库,相同的凭据,那么这样做没有意义,只需在需要时创建连接,然后使用它。如果连接需要不同的连接字符串,则在web.config中为每个字符串创建一个条目



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

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





然后在需要时通过ConfigurationManager读取它们





Then read them via ConfigurationManager when you need them

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





如果你想创建SqlConnection对象并在页面请求之间保留它们等等不要这样做是的,它对性能和资源使用有害,让ado.net通过简单地在需要时创建连接来为您处理连接和连接池。如果您想了解更多信息,可以使用Googleado.net连接池。



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.


这篇关于如何在网页中声明多个连接并在需要时调用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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