user1415780
user1415780

Reputation: 1153

Creating a string in a web.config and use it in a web.api

I am new in the web development world and I would like to create a variable in the web.config file so that I can use it in the .NET portion of the web.api

I found the following tutorials on how to do that :

Setting up connection string in ASP.NET to SQL SERVER

And

http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :

<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />

What should I have in the connectionString and the providerName ?

Upvotes: 8

Views: 34934

Answers (3)

Paul
Paul

Reputation: 1004

For ASP.NET 4.5 Application I'm using appSettings for email configuration. I'm also using connectionStrings

appSettings needs to be included before connectionStrings not before configSections

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="ContactEmail0" value="[email protected]" />
    <add key="ContactEmail1" value="[email protected]" />
  </appSettings>
  <connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
    <add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
  </connectionStrings>
...

Upvotes: 0

Kara Potts
Kara Potts

Reputation: 1062

If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example

<configuration>
  <system.web> ... </system.web>
  <appSettings>
    <add key="MyAppSetting" value="A test value." />
  </appSettings>
</configuration>

This can then be used in your code by getting the value of

System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]

(C#) or

System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")

(VB)

See MSDN for more information, or just search online for "asp.net AppSettings".

Upvotes: 27

Icarus
Icarus

Reputation: 63964

If you don't have a database to connect to (which is what I understood from your question), then you don't even need to have the <connectionStrings> section in your Web.config. That section is only needed if you are going to connect to a database.

If you do use a database, then the connectionString varies depending on several factors such as type of authentication, database product (MS SQL Server, MySQL), type of driver (ODBC, .NET), etc.

The "Provider Name" will depend on the database product that you are using. For example for SQL Server is "System.Data.SqlClient"

You can look at this site for a comprehensive list of database products and connection strings appropriate for each product for different authentication types, drivers used, etc.

Upvotes: 1

Related Questions