使用app.config文件使Windows窗体应用程序可配置 [英] Make Windows Form Application Configurable with app.config file

查看:132
本文介绍了使用app.config文件使Windows窗体应用程序可配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个字典来接收数据库中的值

我想使我的应用程序可配置,以便组合框中仅显示特定值.我想在我的app.config文件中进行更改.

我写了以下内容:一个字典,显示组合框中来自数据库的值.

//在类内部创建Dictionary以在组合框中显示属性

I have created a dictionary that recieves values from a database

I want to make my application configurable that only specefic values must show in the combobox. I want to make changes in my app.config file.

I have written the following: A dictionary that shows the values from a database in my combobox.

//Create Dictionary inside class to display Property in Combobox

  public Dictionary<string, int> SelectdicProperty()
  {
Dictionary<string, int> dicProperty = new Dictionary<string, int>();

      {
      SqlDatabase sqldb = new SqlDatabase(ConnectionString);
IDataReader reader = sqldb.ExecuteReader("PSELPropertyCmb", new object[] { });

          while (reader.Read())
          {
dicProperty.Add(reader["Description"].ToString(), Convert.ToInt32(reader["PropertyID"].ToString()));
          }
      }
      return dicProperty;
  }



//绑定数据库中数据的方法



//Method to bind the data from database

public void SelPropertList()
           Dictionary<string, int> objdic = new Dictionary<string, int>();
           objdic = clsInadmin.SelectdicProperty();

           cmbProperty.DisplayMember = "Key";
           cmbProperty.ValueMember = "Value";
           cmbProperty.DataSource = new BindingSource(objdic, null);
           objdic = null;
       }







private void cmbProperty_SelectedIndexChanged(object sender, EventArgs e)
       {
           int iValue = Convert.ToInt32 (cmbProperty.SelectedValue.ToString());
           IDataReader dr = clsInadmin.FilterProperty(iValue.ToString());
           DataTable dt = new DataTable();
           dt.Load(dr);
           dtgIngenico.DataSource = dt;
       }



这是app.config文件的一部分



Here is the part of app.config file

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </configSections>
  <dataConfiguration defaultDatabase="Connection String"/>
  <connectionStrings>
    <add name="Tool" connectionString="Data Source=TSGDevs01; Initial Catalog=Ing; User ID= User;Password =" ;Connect Timeout=120" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="Unit" value="Blackrock"/>---I added this line to show only blackrock property in the combobox, but is is not working
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>





当我运行我的应用程序时,我只希望Blackrock在字典的组合框中显示.我的问题是,所有其他值都显示在组合框中.

我必须添加或更改什么,以及在哪里?





When I run my application I only want Blackrock to show in the combobox from the dictionary. My problem is, is that all the the other values are showing in the combobox.

What must I add or change, and where?

推荐答案

而不是绑定数据库中的所有值.为什么不在 appconfig 中指定所需内容并从那时开始填充它.然后,您可以在组合框中显示所需的内容.请在下面的链接上查看此链接9.
http://msdn.microsoft.com/en-us/library/system. configuration.configurationmanager.appsettings.aspx [ ^ ]
Instead of binding all the values from the database. Why don''t you specify what you want in the appconfig and the populate it from then. You can then show what you want to in your combobox. check this link below how to9 do this.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx[^]


这篇关于使用app.config文件使Windows窗体应用程序可配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆