Web配置连接设置

时间:2010-09-06 05:07:36

标签: asp.net

我设计了一个网站,通过在每个页面中编写代码,每个页面都与SQL服务器连接....任何人都可以帮助我一次只在Web配置文件中完成这项工作... < / p>

提前致谢..!

4 个答案:

答案 0 :(得分:2)

<connectionStrings>  
        <add name="ConnectionString" connectionString="Data Source=(local); 
        Initial Catalog=DbName; Integrated Security=True" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

答案 1 :(得分:1)

在web.config文件中

    <add name="ConnectionString" connectionString="server=(local); 
database=DbName; uid=*username*; password=*password*;" 
providerName="System.Data.SqlClient" />

答案 2 :(得分:0)

请试试这个:

在网络配置中:

<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

阅读.cs页面

System.Configuration.Configuration rootWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
            System.Configuration.ConnectionStringSettings connString;
            if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
            {
                connString =
                    rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
                if (connString != null)
                    Console.WriteLine("Northwind connection string = \"{0}\"",
                        connString.ConnectionString);
                else
                    Console.WriteLine("No Northwind connection string");
            }

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

答案 3 :(得分:0)

<connectionStrings> <add name="SourceNet_R01ConnectionString" connectionString="Data Source=10.134.72.61;Initial Catalog=SourceNet_R01;Integrated Security=True" providerName="System.Data.SqlClient"/> <add name="SourceNet_R01ConnectionString1" connectionString="Data Source=10.134.72.61;Initial Catalog=SourceNet_R01;User ID=dev;Password=vcel92" providerName="System.Data.SqlClient"/> </connectionStrings>

对于检索可以使用 ConfigurationManager.ConnectionStrings[1].ConnectionString