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 回答