1

以前は、asp.netWebサイトでのみ作業していました。ASP.NET Webサイトでは、接続文字列がweb.configファイルにあることを知っています。

私の問題は、データベースに接続する必要のあるVB.NETアプリケーションの操作を開始したことです。接続文字列はどのように作成され、どこに配置すればよいですか?

ありがとう!

app.configファイル全体は次のとおりです。

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <connectionStrings>
    <add name="dbAsthmaConnectionString" connectionString="Data Source=9300-00\SQLEXPRESS;Initial Catalog=dbStore;Persist Security Info=True;User ID=johnsmith;Password=1234" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
         <listeners>
           <add name="FileLog"/>
             <!-- Uncomment the below section to write to the Application Event Log -->
             <!--<add name="EventLog"/>-->
         </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
           initializeData="FileLogWriter"/>
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
</configuration>
4

4 に答える 4

3

ここに例があります:1-あなたのapp.configは次のようになります:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Amlakconn" connectionString ="Data Source=KHASHAYAR-PC\SQLEXPRESS;Initial Catalog=Amlak;Integrated Security=True"/>
  </connectionStrings>
</configuration>

2-そしてあなたのコードではこの方法で接続にアクセスできます:

private string conn = ConfigurationManager.ConnectionStrings["Amlakconn"].ConnectionString;

試してみてください;)

于 2012-05-02T19:12:28.313 に答える
2

他の回答は、接続文字列を配置する場所を示しています:http: //msdn.microsoft.com/en-us/library/ms254494 (v=vs.80).aspx

これは、接続文字列を作成する最も簡単な方法です。

a)テキストファイルを作成し、拡張子の名前をTXTからUDLに変更して、Enterキーを押します。

b)UDLファイルをダブルクリックし、[SQLServerのOLEDBプロバイダー]を選択します>[次へ]>データベースサーバー名を入力します>データベースを選択し、[接続のテスト]をクリックします。

ここに画像の説明を入力してください

c)テストに合格したら、UDLファイルを閉じてメモ帳で開きます。太線は、接続文字列です。

[oledb]; この行以降はすべてOLEDBinitstring
Provider=SQLOLEDB.1です。Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = YourDatabase; Data Source = SQLEXPRESS

于 2012-05-02T23:22:25.337 に答える
0

ASP.NET以外のアプリケーションも構成ファイルを使用できます。名前は付けられていませんweb.configConnectionStringsこのファイルは、セクションを含め、ASP.NETで知っている構造とまったく同じです。コンテンツをコピーしてweb.config、必要なセクションをに貼り付けることができますapp.config。プロジェクトでは次のように表示されapp.configますが、binフォルダーでは、実行可能ファイル名(exe拡張子付き)に「.config」を加えた名前が付けられます。

このファイルを作成するには、プロジェクトに移動しAdd -> New Item、[アプリケーション構成ファイル]を選択します。接続文字列にアクセスするには、<ConnectionString>セクションを<configuration>セクションに作成/コピーしてから、次のコードを使用します。

string conStr = ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conStr);

`System.Configuration'アセンブリへの参照を追加する必要があります。

于 2012-05-02T17:50:34.757 に答える
0

あなたがウェブアプリプロジェクトに取り組んでいるならそれは同じです!これをweb.configファイルに入れることができます。プロジェクトがwinappの場合は、app.configファイルをプロジェクトに追加して、そこに接続文字列を追加できます。

于 2012-05-02T17:53:40.543 に答える