列表<T>将 foreach 循环内的所有项目覆盖到最后一个值 [英] List&lt;T&gt; overwrites all the items inside a foreach loop to the last value

查看:31
本文介绍了列表<T>将 foreach 循环内的所有项目覆盖到最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build a windows application in which there is a combo box and during the Load(), I'm population this combo box with all the connection strings availabe in my app.Config file.

这是 app.Config 片段:

Here is the app.Config snippet:

<!-- 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 获取所有值并将其显示到组合框.

I have created a class CompareResult in which I'm trying to get all the values from the app.Config and displaying it to the combo box.

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 的片段

The problem is when I'm trying to bind it with the Combo box, it is showing three records as per the app.Config file, but all the records have same value as "SQLConnect-3". I'm unable to find the reason why the List got updated with "SQLConnect-3" for all the records. Please help to fix the error. Here is the snippet where I'm binding this list to 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";

}

推荐答案

这个:

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

conStr.Add(conn);

在每次迭代时覆盖您的 ConnectionString 引用.您需要在每个循环中分配一个新的 ConnectionString:

Overwrites your ConnectionString reference each iteration. You need to allocate a new ConnectionString within each loop:

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);
    }
}

这篇关于列表<T>将 foreach 循环内的所有项目覆盖到最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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