清单< T>覆盖foreach循环内的所有项目的最后一个值 [英] List<T> overwrites all the items inside a foreach loop to the last value

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

问题描述

我试图建立一个Windows应用程序,其中有负载(在组合框和),我是人群都在我的app.config文件中的连接字符串速效该组合框。



下面是在app.config片段:

 <  - 添加多个服务器在连接字符串 - > 
<&是connectionStrings GT;
<添加名称=的SQLConnect-1
的connectionString =数据源= SAHIL;初始目录= RecordComparisonTool;集成安全性= SSPI
的providerName =System.Data.SqlClient的/ >

<添加名称=的SQLConnect-2
的connectionString =数据源= SAHIL;初始目录= RecordComparisonTool;用户ID =测试;密码= 12123;集成安全性= SSPI
的providerName =System.Data.SqlClient的/>

<添加名称=的SQLConnect-3
的connectionString =数据源= SAHIL;初始目录= RecordComparisonTool;用户ID =测试;密码= 32315;集成安全性=真
的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;



我创建了一个类CompareResult中,我试图从应用程序中的所有值。配置并显示到组合框。

 命名空间RecordComparisonTool_Win 
{
公共类的ConnectionString
{
公共字符串名称{;组; }
公共字符串conString {搞定;组; }
公共字符串提供商{搞定;组; }
}

公共类CompareResult
{

公开名单<&ConnectionString中GT;的getConnection()
{
名单,LT;的ConnectionString>构造=新名单<&ConnectionString中GT;();
的ConnectionString康恩=新的ConnectionString();

的foreach(在ConfigurationManager.ConnectionStrings ConnectionStringSettings CSS)
{
如果(!(css.Name ==LocalSqlServer这个|| css.Name ==LocalMySqlServer))
{
conn.name = css.Name;
conn.conString = css.ConnectionString;
conn.provider = css.ProviderName;

conStr.Add(康涅狄格州);
}
}
返回构造;
}

}
}



问题当我想的是与组合框绑定,它显示三个记录按app.config文件,但所有的记录都具有相同的价值的SQLConnect-3。我无法找到,为什么清单得到了与的SQLConnect-3更新了所有记录的原因。请帮忙修复错误。这里是我结合这个列表组合框

 保护无效LoadConnection()
{$ B $片段b compareResult compareResult =新compareResult();

名单,LT;的ConnectionString>的connectionString =新的List<&ConnectionString中GT;();
的connectionString = compareResult.GetConnection();

cbTokenLeft.DataSource =的connectionString;

cbTokenLeft.DisplayMember =名;
cbTokenLeft.ValueMember =conString;

}


解决方案

本:

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

conStr.Add(康涅狄格州);



您覆盖的ConnectionString 引用每个迭代。你需要分配一个新的的ConnectionString 每个循环中:

 的foreach(在ConfigurationManager.ConnectionStrings ConnectionStringSettings CSS)
{
如果(!(css.Name ==LocalSqlServer这个|| css.Name ==LocalMySqlServer))
{
变种康恩=新的ConnectionString
{
NAME = css.Name;
conString = css.ConnectionString;
提供商= css.ProviderName;
}
conStr.Add(康涅狄格州);
}
}


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.

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>

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

    }
}

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";

}

解决方案

This:

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

conStr.Add(conn);

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

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

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