繁体   English   中英

List.Add方法不起作用

[英]List.Add Method is not working

public class abc
    {
        public abc(int ID, string Date, string jobtype, string command)
        {
            this.ID = ID;
            this.Date = Date;
            this.jobtype = jobtype;
            this.command = command;
        }

        public int ID { get; set; }
        public string Date { get; set; }
        public string jobtype { get; set; }
        public string command { get; set; }
    }

    public List<abc> getdata()
    {
        List<abc> data = new List<abc>();
        SqlCommand cmd = new SqlCommand("SELECT * FROM [Table]",connection1);
        using (connection1)
        {
            connection1.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    **data.Add(sdr["job_type"]);**
                }
                connection1.Close();
                return data;
            }
        }
    }

   private DataTable Getdata(SqlCommand cmd)

    {
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection1;

        try
        {
            connection1.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }

        catch
        {
            return null;
        }

        finally
        {
            connection1.Close();
            sda.Dispose();
            connection1.Dispose();
        }
    }

上面的代码,在data.Add(sdr [“ job_type”]);上出现错误。 哪里:

错误2'System.Collections.Generic.List的最佳重载方法匹配具有无效参数错误3参数1:无法从'对象'转换为'WindowsFormsApplication1.Form1.abc'

我尝试通过Google找到一些解决方案,但没有找到一个好运。 我是C#的新手,所以有人可以告诉我我做错了什么吗? 该代码是我在搜索过程中遇到的一些示例。

除了将数据库中的数据放入如上所示的列表之外,还有其他方法吗? 谢谢。

您不能简单地将数据库对象转换为abc类。 您将需要阅读每一列并创建一个新的abc对象,因为您的列表只能包含对象abc

例如(我是根据MySQLReader知识进行此操作的,可能有所不同)

while (sdr.Read())
{
    data.Add(new abc((int)sdr["ID"],
    (string)sdr["Date"],
    (string)sdr["job_type"],
    (string)sdr["command"]));
}

显然,列名需要重命名为您的确切列名。

您将必须从数据库中读取的实际行中创建一个新的abc对象。

就像是

data.Add(new abc(
    Convert.ToInt32(sdr["ID"]), 
    Convert.ToString(sdr["DATE"]), 
    Convert.ToString(sdr["job_type"]), 
    Convert.ToString(sdr["command"]))
    );

可能是这样

您需要通过创建类对象来指定属性

data.Add(new abc() {job_type1=sdr["job_type"].ToString()});

要么

将数据传递给班级Abc构造函数

data.Add(new abc(sdr["job_type"].ToString()));

以供参考

通过MSDN

 using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                abc temp = new abc();

                while (sdr.Read())
                {
                     temp = new abc();
                     temp.ID =  Convert.ToInt32(sdr["ID"]);
                      temp.Date =  Convert.ToDate(sdr["Date"]);
                       temp.jobtype =  Convert.ToString(sdr["job_type"]);
                     temp.command =  Convert.ToString(sdr["command"]);
                    data.Add(temp);
                }
                connection1.Close();
                return data;
            }

我建议修改abc的构造函数,以使其将SqlDataReader作为参数并在构造函数内部进行分配工作:

public class abc
{
    private enum Column
    {
        ID = 1,
        Date = 2,
        jobtype = 3,
        command = 4
    }

    public abc(SqlDataReader reader)
    {
        ID = sdr.GetFieldValue<int>[Column.ID];
        Date = sdr.GetFieldValue<string>[Column.Date];
        jobtype = sdr.GetFieldValue<string>[Column.jobtype];
        command = sdr.GetFieldValue<string>[Column.command];

        // or
        //ID = (int)sdr["ID"];
        //Date = (string)sdr["Date"];
        //jobtype =(string)sdr["jobtype"];
        //command = (string)sdr["command"];
    }

    public int ID { get; set; }
    public string Date { get; set; }
    public string jobtype { get; set; }
    public string command { get; set; }
}

这样,您的循环就很干净了,并且对解析/广播 (如果有)进行了封装:

while (sdr.Read())
{
    data.Add(new abc(sdr));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM