本文介绍了为什么我的Visual Studio 10停止对SQL连接字符串使用App.Config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用WPF和SQL Server 2008 R2的有效数据库应用程序,两年来一直从App.Config文件获取其SQL Server连接字符串.几天前,在一台开发机上,它开始忽略App.Config文件的connectionString,现在正在使用其他位置的字符串(看起来像settings.settings或DBML文件).

I have a working database application using WPF and SQL Server 2008 R2, which for two years has been getting its SQL Server connection string from the App.Config file. A few days ago on one dev machine, it started ignoring the App.Config file's connectionString, and is now using a string from somewhere else (looks like either settings.settings, or the DBML file).

为什么会发生这种情况,我如何才能停止这样做呢?

Why might this be happening, and how can I get it to stop doing that?

app.config像这样启动:

The app.config starts out like this:

<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <connectionStrings>
    <add name="DronzApp.Properties.Settings.DronzAppConnectionString"
     connectionString="Server=dronz.db.123.dronzdbserver.com;Database=dronzdb;User ID=dronz;Password=secretsauce;"
     providerName="System.Data.SqlClient" />
 </connectionStrings>

感谢您双方的建议,以获取更多信息和查找位置.我不知道WPF应用程序在哪里可以获取应在App.Config或其他任何地方查找的信息,但是在我了解到这一点之前,这里还有更多内容:

Thanks for the suggestions from both of you for more info and where to look. I don't know where a WPF App gets the information that it ought to look in App.Config, or anyplace else, but until I learn that, here are some more pieces:

我的程序要做的第一件事就是测试数据库(现在失败了).在此之前,它会调用自动生成的函数InitializeComponent(),其自动生成的代码为:

One of the first things my program does is test the database (which now fails). Right before it does that, it calls the auto-generated function InitializeComponent(), whose auto-generated code is:

        /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public void InitializeComponent() {
        if (_contentLoaded) {
            return;
        }
        _contentLoaded = true;
        System.Uri resourceLocater = new System.Uri("/DronzApp;component/ui/startwindow.xaml", System.UriKind.Relative);

        #line 1 "..\..\..\UI\StartWindow.xaml"
        System.Windows.Application.LoadComponent(this, resourceLocater);

        #line default
        #line hidden
    }

我的启动窗口构造函数调用InitializeComponent();.然后使用以下行测试数据库:

My start window constructor calls InitializeComponent(); and then tests the database with the line:

int hmm = App.db.Dronz_FooTable.Count();

int hmm = App.db.Dronz_FooTable.Count();

其中App.db是在app.xaml.cs文件中定义为以下内容的数据上下文:

where App.db is a data context defined in the app.xaml.cs file as:

公共静态DronzDataDataContext db = new DronzDataDataContext();

public static DronzDataDataContext db = new DronzDataDataContext();

其中DronzDataDataContext是通过LINQ-to-SQL在自动生成的代码中定义的,例如:

where DronzDataDataContext is defined in auto-generated code by LINQ-to-SQL such as:

公共局部类DronzDataDataContext:System.Data.Linq.DataContext...

public partial class DronzDataDataContext : System.Data.Linq.DataContext...

    public DronzDataDataContext() :
            base(global::DronzApp.Properties.Settings.Default.DronzConnectionString, mappingSource)
    {
        OnCreated();
    }

过去曾经注意app.config文件,现在却不这样做.当我发现数据库异常(这是有关数据库版本的问题,因为它试图使用错误的SQL Server)并查看连接字符串时,它是在向错误的SQL Server询问文件而不是正确的连接字符串.它使用的连接字符串似乎与导入数据库模式时Linq-to = SQL创建的DBML文件匹配,或者与settings.settings中的字符串匹配(这是我不太了解它来自何处的文件,或者我应该做什么或不做什么).

Which used to heed the app.config file, and now doesn't. When I catch the DB exception (which is about a DB version problem because it is trying to use the wrong SQL server) and look at the connection string, it is asking the wrong SQL Server for a file instead of the correct connection string. The connection string it is using seems to match either the DBML file that Linq-to=SQL created when the database schema was imported, or a string in settings.settings (which is a file I don't really understand where it came from or what I'm supposed to do or not do with it).

推荐答案

好,我似乎已经找到并解决了问题,但我不知道是什么原因造成的.

Ok, I seem to have found and fixed the problem, but I don't know what caused it, exactly.

引用Config文件的内容实际上位于.proj文件中,并且以某种方式对其进行了更改,以在项目根目录而不是App子文件夹中添加对app.config的引用.通过阅读有关app.config的其他讨论,我认为VS2010也许是在查看GUI中的Project设置时自动完成的.从.proj文件中手动删除该行XML,使其可以查找并使用指向app.config文件实际位置的先前版本.由于我在项目根目录中没有app.config文件,新的第一行被编辑为说它位于.proj文件中,因此似乎要去查看settings.settings,在其中找到connectionString值使用Linq将数据库文件导入到SQL时文件所在的位置.

What references the Config file is actually in the .proj file, and somehow it changed to add a reference to app.config at the project root instead of in an App subfolder. From reading other discussion of app.config, I think VS2010 did this automatically perhaps when I was looking at the Project settings in the GUI. Deleting that line of XML manually from the .proj file allowed it to find and use the previous version which points to where the app.config file actually is. Since I didn't have an app.config file in the project root where the new first line was edited to say it was in the .proj file, it seems to have fallen to looking at settings.settings, where it found the connectionString value where the file was when I imported the database file using Linq to SQL.

这篇关于为什么我的Visual Studio 10停止对SQL连接字符串使用App.Config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 16:26