开发者

C# App.Config File

开发者 https://www.devze.com 2023-01-24 05:56 出处:网络
Bear with me I am pretty new to c#. Right now I am looking at a app.config file in the main project. Here is the code:

Bear with me I am pretty new to c#. Right now I am looking at a app.config file in the main project. 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=neut开发者_C百科ral, 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. 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. 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.AppSettings["SettingsKey"];


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

You can find out more here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消