连接字符串和配置管理器

时间:2013-09-20 23:32:15

标签: .net visual-studio-2010 visual-studio sql-server-2005 connection-string

我有一个别人的项目,并且有这行代码:

ConnectionStringSettings myConnectionStringSetting = ConfigurationManager.ConnectionStrings[0];

由于某种原因返回:

ConfigurationManager.ConnectionStrings[0] is returning {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}                System.Configuration.ConnectionStringSettings

据我所知,我没有安装selexpress,我的web.config使用不同的连接字符串指向我的服务器。在哪里可以找到VS 2010中所有连接字符串的列表?

对整个解决方案进行查找不会返回sql express的结果。

2 个答案:

答案 0 :(得分:4)

尝试在ConnectionsString之前插入< clear /> 标记。

 <connectionStrings>
  <clear/>
  <!--You connectionString go here-->
 </connectionStrings>

答案 1 :(得分:0)

About Connection Strings and Configuration Files

About Connection Strings & the Web.config File

来自MSDN的示例:

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");
        }