8

私は Web 開発の世界に不慣れで、web.config ファイルに変数を作成して、web.api の .NET 部分で使用できるようにしたいと考えています。

その方法については、次のチュートリアルを見つけました。

ASP.NET で SQL SERVER への接続文字列を設定する

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

次の質問があります。文字列を接続するデータベースがありません (コードを使用せずに文字列を簡単に変更できるように、Web 構成でのみ使用します。次の方法:

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

connectionStringとには何が必要providerNameですか?

4

3 に答える 3

26

あなたが何をしたいのか理解できれば、接続文字列をまったく使用したくないようです。代わりに、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」をオンラインで検索してください。

于 2013-04-16T10:48:53.080 に答える
1

接続するデータベースがない場合 (これは私があなたの質問から理解したものです<connectionStrings>) Web.config、. このセクションは、データベースに接続する場合にのみ必要です。

データベースを使用する場合、connectionString は、認証の種類、データベース製品 (MS SQL Server、MySQL)、ドライバーの種類 (ODBC、.NET) などのいくつかの要因によって異なります。

「プロバイダー名」は、ご利用のデータベース製品によって異なります。たとえば、SQL Server の場合は"System.Data.SqlClient"

このサイトでは、データベース製品の包括的なリストと、さまざまな認証タイプ、使用されるドライバーなどの各製品に適した接続文字列を確認できます。

于 2013-04-08T13:38:36.597 に答える
0

ASP.NET 4.5 アプリケーションの場合、電子メールの構成に appSettings を使用しています。私もconnectionStringsを使用しています

configSections の前ではなく、connectionStrings の前に appSettings を含める必要があります。

<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="service@davincispainting.com" />
    <add key="ContactEmail1" value="estimate@davincispainting.com" />
  </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>
...
于 2015-04-25T19:09:32.587 に答える