List <t>将foreach循环中的所有项覆盖为最后一个值

时间:2015-04-26 08:35:02

标签: c# winforms list combobox app-config

我正在尝试构建一个有一个组合框的Windows应用程序,在Load()期间,我正在使用我的app.Config文件中可用的所有连接字符串这个组合框。

这是app.Config片段:

<!-- Adding Multiple Servers in Connection String-->
<connectionStrings>
   <add name="SQLConnect-1"
         connectionString="Data Source=SAHIL; Initial Catalog=RecordComparisonTool; Integrated Security=SSPI"
         providerName="System.Data.SqlCLient"/>

   <add name="SQLConnect-2"
     connectionString="Data Source=SAHIL; Initial Catalog=RecordComparisonTool; User Id=test; Password=12123; Integrated Security=SSPI"
     providerName="System.Data.SqlCLient"/>

   <add name="SQLConnect-3"
     connectionString="Data Source=SAHIL; Initial Catalog=RecordComparisonTool; User Id=test; Password=32315;  Integrated Security=True"
     providerName="System.Data.SqlCLient"/>
 </connectionStrings>

我创建了一个CompareResult类,我试图从app.Config获取所有值并将其显示在组合框中。

namespace RecordComparisonTool_Win
{
   public class ConnectionString
   {
        public string name { get; set; }
        public string conString { get; set; }
        public string provider { get; set; }
    }

    public class CompareResult
    {

        public List<ConnectionString> GetConnection()
        {
            List<ConnectionString> conStr = new List<ConnectionString>();
            ConnectionString conn = new ConnectionString();

            foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
            {
                if (!(css.Name == "LocalSqlServer" || css.Name == "LocalMySqlServer"))
                {
                    conn.name = css.Name;
                    conn.conString = css.ConnectionString;
                    conn.provider = css.ProviderName;

                    conStr.Add(conn);
                }
            }
            return conStr;
        }

    }
}

问题是当我尝试将其与组合框绑定时,它根据app.Config文件显示三条记录,但所有记录都具有与“SQLConnect-3”相同的值。对于所有记录,我无法找到列表更新为“SQLConnect-3”的原因。请帮助修复错误。这是我将此列表绑定到ComboBox

的片段
protected void LoadConnection()
{
    CompareResult compareResult = new CompareResult();

    List<ConnectionString> connectionString = new List<ConnectionString>();
    connectionString = compareResult.GetConnection();

    cbTokenLeft.DataSource = connectionString;

    cbTokenLeft.DisplayMember = "name";
    cbTokenLeft.ValueMember = "conString";

}

3 个答案:

答案 0 :(得分:1)

此:

conn.name = css.Name;
conn.conString = css.ConnectionString;
conn.provider = css.ProviderName;

conStr.Add(conn);

每次迭代都会覆盖您的ConnectionString引用。您需要在每个循环中分配一个新的ConnectionString

foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
{
    if (!(css.Name == "LocalSqlServer" || css.Name == "LocalMySqlServer"))
    {
        var conn = new ConnectionString
        {
           name = css.Name;
           conString = css.ConnectionString;
           provider = css.ProviderName;
        }
        conStr.Add(conn);
    }
}

答案 1 :(得分:0)

将行line-height移到最里面的循环中。它是一个引用类,您将一遍又一遍地将相同的实例添加到列表中。

ConnectionString conn = new ConnectionString();

答案 2 :(得分:0)

由于您在循环中实例化 public List<ConnectionString> GetConnection() { List<ConnectionString> conStr = new List<ConnectionString>(); foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings) { if (!(css.Name == "LocalSqlServer" || css.Name == "LocalMySqlServer")) { ConnectionString conn = new ConnectionString(); conn.name = css.Name; conn.conString = css.ConnectionString; conn.provider = css.ProviderName; conStr.Add(conn); } } return conStr; } ,因此对所有迭代使用(并覆盖)相同的实例。您需要在循环中实例化,以便每次迭代都获得一个新实例:

ConnectionString

您也可以使用LINQ表示相同的代码:

foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
{
    if (!(css.Name == "LocalSqlServer" || css.Name == "LocalMySqlServer"))
    {
        ConnectionString conn = new ConnectionString();

        conn.name = css.Name;
        conn.conString = css.ConnectionString;
        conn.provider = css.ProviderName;

        conStr.Add(conn);
    }
}