2013-04-08 13 views
4

使用它,我在Web開發領域是新的,我想在web.config文件中創建一個變量,這樣我可以在使用它。在web.api創建在一個web.config一個字符串,並以web.api

的NET部分我發現怎麼做以下教程:

Setting up connection string in ASP.NET to SQL SERVER

而且

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

我有以下問題,我不噸有一個數據庫字符串連接(我只用它的Web配置,這樣我可以很容易地改變字符串,而不必去通過代碼。所以假設我通過以下方式使用它:

​​

我應該有在connectionStringproviderName

回答

16

如果我理解你想要什麼要做,聽起來好像你根本不想使用連接字符串。而應使用web.config文件的「應用程序設置」部分。例如

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

然後可以在代碼中通過獲取的

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

(C#)的值或

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

(VB)

參見MSDN爲使用更多信息,或只是在網上搜索「asp.net AppSettings」。

1

如果你沒有一個數據庫連接(這是我從你的問題理解的),那麼你甚至不需要有<connectionStrings>部分在Web.config。只有在您要連接到數據庫時才需要該部分。

如果你使用一個數據庫,那麼的connectionString取決於幾個因素,如身份驗證類型,數據庫產品(MS SQL Server,MySQL發),類型的驅動程序(ODBC,.NET)等

變化

「提供者名稱」將取決於您正在使用的數據庫產品。例如,對於SQL Server是"System.Data.SqlClient"

你可以看一下this site所使用的數據庫產品,並適用於每個產品針對不同身份驗證類型的連接字符串,驅動程序的完整列表等

0

對於我使用的appSettings電子郵件配置ASP.NET 4.5應用。我還使用的ConnectionStrings

的appSettings需要包含的ConnectionStrings之前

<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> 
... 
+0

'的appSettings需要包含的ConnectionStrings之前不前configSections不configSections'前 - 也許你確實出於某種原因,但我有一個appSettings低於connectionStrings的應用程序,完美無瑕。很確定訂單沒有關係。 – 2015-07-08 14:07:48