0

App.configに次のコードがありますが、その下のコードを機能させるにはどうすればよいですか????

configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WpfDatabind3.Properties.Settings.cbfSQL1ConnectionString"
            connectionString="Data Source=STEPHANS-PC\SQLEXPRESS;Initial Catalog=cbfSQL1;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

App.configで接続文字列にアクセスする方法がわかりません。

DataSet myDataSet;

private void OnInit(object sender, EventArgs e)
{
  string mdbFile = Path.Combine(AppDataPath, "BookData.mdb");
  string connString = string.Format(
      "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile);
  OleDbConnection conn = new OleDbConnection(connString);
  OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM BookTable;", conn);

  myDataSet = new DataSet();
  adapter.Fill(myDataSet, "BookTable");

  // myListBox is a ListBox control. 
  // Set the DataContext of the ListBox to myDataSet
  myListBox.DataContext = myDataSet;
}
4

1 に答える 1

9

プロジェクトへの参照を追加System.Configuration.dllします。次に、ConfigurationManagerクラスを使用します。名前で接続文字列設定を取得できます。

string connString = ConfigurationManager
   .ConnectionStrings["WpfDatabind3.Properties.Settings.cbfSQL1ConnectionString"]
   .ConnectionString;
于 2012-11-19T17:21:01.477 に答える