foreach语句不能对'gerant'类型的变量进行操作,因为'gerant'不包含'GetEnumerator'的公共定义

时间:2015-12-21 14:58:52

标签: c# foreach public

你好我尝试创建一个名为'gerant'的对象

 class gerant
{
    public double CIN_GERANT, NUM_TEL_GERANT, MOBILE_GERANT;
    public string NOM_GERANT, PRENOM_GERANT, ADRESSE__GERANT, MAIL_GERANT, VILLE_GERANT;
    public int CP_GERANT;
    public DateTime DATE_GERANT;

    public gerant(double _Cin_gerant, string _Nom_Gerant, string _Prenom_Gerant, string _Adresse_Gerant, double _Num_Tel_Gerant, string _Mail_Gerant, double _Mobile_Gerant, int _cp_gerant, string _ville_gerant, DateTime _date_gerant)
    {
        this.CIN_GERANT = _Cin_gerant;
        this.NOM_GERANT = _Nom_Gerant;
        this.PRENOM_GERANT = _Prenom_Gerant;
        this.ADRESSE__GERANT = _Adresse_Gerant;
        this.NUM_TEL_GERANT = _Num_Tel_Gerant;
        this.MAIL_GERANT = _Mail_Gerant;
        this.MOBILE_GERANT = _Mobile_Gerant;
        this.CP_GERANT = _cp_gerant;
        this.VILLE_GERANT = _ville_gerant;
        this.DATE_GERANT = _date_gerant;
    }

    public gerant getinfogerant()
    {
        gerant gerer = null;
        string sql_gerant = "select CIN,NOM,PRENOM,ADRESS_PERSONNEL,NUM_TEL,MAIL,MOBILE,CP_GERANT,VILLE_GERANT,DATE_CIN from GERANT";
        connexion connect = new connexion();
        OleDbConnection connection = connect.getconnexion();
        // try
        //{
        connection.Open();
        OleDbCommand cmd = new OleDbCommand(sql_gerant, connection);
        OleDbDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            gerer = new gerant(reader.GetDouble(0),
                reader.GetString(1),
                reader.GetString(2),
                reader.GetString(3),
            reader.GetDouble(4),
                reader.GetString(5),
                reader.GetDouble(6),
                reader.GetInt32(7),
                reader.GetString(8),
                reader.GetDateTime(9)
                );

        }
        connection.Close();
        return gerer;

    }
}

但是当我尝试用gerant填充我的组合框时,我尝试插入此代码

 foreach(Modele.gerant ligne in liste_gerant)
        {

        }

但我为我犯了这个错误 foreach语句不能对'gerant'类型的变量进行操作,因为'gerant'不包含'GetEnumerator'的公共定义

我该如何解决?

2 个答案:

答案 0 :(得分:0)

根据您的评论。 gerant类不是任何类型的集合,并且您的getinfogerant不会返回集合。

所以,问题是你试图迭代一个值。

答案 1 :(得分:0)

您在getinfogerant中使用的查询我想您希望它在您的数据库中获得所有“gerant”,而不只是一个?

然而现在你只是回归第一个。您应该将其返回类型更改为List并将if(reader.Read())转换为while循环以填充列表,然后在结尾处返回整个列表。