如何在web.config中设置和调用全局连接 [英] how set and call global connection in web.config

查看:72
本文介绍了如何在web.config中设置和调用全局连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在web.config中

In web.config

<connectionStrings>
  <add name="Connection" connectionString="Data Source=ESS-8\\SQLEXPRESS;Initial Catalog=Inventory1;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>



在页面上



on page

String con = ConfigurationManager.ConnectionStrings["Connection"].ToString();



错误是



error is

'string' does not contain a definition for 'Open' and no extension method 'Open' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

推荐答案

阅读:http://msdn.microsoft.com/en-us/library/ms178411.aspx [

是的,您可以轻松地从web.config调用连接名称

例如:

像这样声明web.config中的连接字符串路径
yes u can call easily connection name from web.config

for e.g:

Declare connectionstring path in web.config like that
<connectionStrings>
        <add name="connStringForAutoComplete" connectionString="Data Source=SCANPC013\SQLEXPRESS;Initial Catalog=SMART_FKS;User ID=sa;Password=scan"/>
    </connectionStrings>




然后在您的代码中将此connectionString名称称为




then call this connectionString name in ur code

string connStr = ConfigurationManager.ConnectionStrings["connStringForAutoComplete"].ToString();



在您想使用的任何地方使用此连接字符串对象



use this connection string object anywhere u want to use


In web.config
<configuration>

   <system.web>
    <compilation defaultlanguage="c#" debug="true" />
  </system.web>

   <appsettings />
    <connectionstrings>
<add name="Connection" connectionstring="Data Source=(server);Initial Catalog=database name;Persist Security Info=true; User Id=sa;Password=1234;" providername="System.Data.SqlClient" />


</connectionstrings>


</configuration>


In Login.aspx.cs


In Login.aspx.cs

String conn = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ToString();


这篇关于如何在web.config中设置和调用全局连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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