简体   繁体   English

C#App.Config文件

[英]C# App.Config File

Bear with me I am pretty new to c#. 忍受我,我是C#的新手。 Right now I am looking at a app.config file in the main project. 现在,我正在查看主项目中的一个app.config文件。 Here is the code: 这是代码:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=vsfe2wed2/>
  </configSections>
  <dataConfiguration defaultDatabase="Testing"/>
  <connectionStrings>
    <add name="Testing" connectionString="Data Source=Test123;Initial Catalog=TestData;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

If I have nothing else written in my whole solution but just an empty class and then this .config file. 如果我在整个解决方案中没有其他东西,只剩下一个空类,然后是这个.config文件。 What exactly is this code doing? 这段代码到底在做什么? IS this establishing a connection to the database or is this just setting up what I need to establish the connection to the database? 这是建立到数据库的连接,还是只是建立我需要建立到数据库的连接?

Thanks 谢谢

The app.config (or web.config for web projects) only contains configuration information. app.config(或用于Web项目的web.config)仅包含配置信息。 It doesn't do any work directly, it's just a central location for all the configuration values to go. 它不直接执行任何工作,它只是所有配置值的存放位置。

You can retrieve settings from the config file to use in your own project by using the ConfigurationManager class. 您可以使用ConfigurationManager类从配置文件中检索设置以在自己的项目中使用。

ConfigurationManager.AppSettings["SettingsKey"];

This is not code. 这不是代码。 It's just a config file containing settings and unless you have some code using them it won't do anything. 这只是一个包含设置的配置文件,除非您有使用它们的代码,否则它不会做任何事情。 It is not establishing any connection to the database, it's just externalizing the connection string so that you don't have to hardcode it in your application when you write the data access layer. 它并没有建立与数据库的任何连接,只是外部化了连接字符串,因此您在编写数据访问层时不必在应用程序中对其进行硬编码。

This is just a stubbed out/sample config file, but nothing will get used unless your code actually talks to the database it has specified. 这只是一个存根/示例配置文件,但是除非您的代码实际与它指定的数据库进行对话,否则什么都不会使用。 It's set up to talk to the database 'Testing' with the connection string "Data Source=Test123;Initial Catalog=TestData;Integrated Security=True;" 它已设置为使用连接字符串“ Data Source = Test123; Initial Catalog = TestData; Integrated Security = True;”与数据库“ Testing”对话。

You can find out more here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx 您可以在此处找到更多信息: http : //msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM