【问题标题】:DataTable Row Count is EmptyDataTable 行数为空
【发布时间】:2016-01-12 14:04:03
【问题描述】:

我正在尝试获取从 OleDB 查询中检索到的联系人列表,将它们添加到列表中,然后将列表加载到 DataTable 中。当我计算列表中的项目数时,它会得到正确的数字(27000)。

但是,当我计算 DataTable 中的行数时,结果为 0。执行此操作后,我想使用 FileHelpers 将 DataTable 写入 CSV,但是 CSV 文件为空。

这是我正在使用的代码;

var listOfContacts = new List<Contact>();
using (OleDbConnection dbfCon = new OleDbConnection(dbfConstr))
{
    dbfCon.Open();
    var dbfCmd = new OleDbCommand(@"SELECT ct_id, ct_cmpid, ct_empid,
    ct_pplid, ct_cntid, ct_pplnm, ct_date, ct_time, ct_type, ct_doneby, ct_desc
    FROM contacts", dbfCon);
    using (var myReader = dbfCmd.ExecuteReader())
    {                       
        while (myReader.Read())
        {
            var newContact = new Contact()
            {
                ContactID = Convert.ToInt32(myReader[0]),
                CompanyID = Convert.ToInt32(myReader[1]),
                EmployeeID = Convert.ToInt32(myReader[2]),
                PersonID = Convert.ToInt32(myReader[3]),
                ContractID = Convert.ToInt32(myReader[4]),
                PersonName = myReader[5].ToString(),
                ContactDate = Convert.ToDateTime(myReader[6]),
                ContactTime = Convert.ToDateTime(myReader[7]),
                TypeOfContact = myReader[8].ToString(),
                ContactMadeBy = myReader[9].ToString(),
                ContactDescription = myReader[10].ToString(),                           
            };
            listOfContacts.Add(newContact);
        }
    }

    DataTable dTable = new DataTable();

    dTable.Columns.Add("ContactID");
    dTable.Columns.Add("CompanyID");
    dTable.Columns.Add("EmployeeID");
    dTable.Columns.Add("PersonID");
    dTable.Columns.Add("ContractID");
    dTable.Columns.Add("PersonName");
    dTable.Columns.Add("ContactDate");
    dTable.Columns.Add("ContactTime");
    dTable.Columns.Add("TypeOfContact");
    dTable.Columns.Add("ContactMadeBy");
    dTable.Columns.Add("ContactDescription");

    MessageBox.Show(listOfContacts.Count.ToString());

    foreach (var contact in listOfContacts)
    {
        var newRow = dTable.NewRow();
        newRow["ContactID"] = contact.ContactID;
        newRow["CompanyID"] = contact.CompanyID;
        newRow["EmployeeID"] = contact.EmployeeID;
        newRow["PersonID"] = contact.PersonID;
        newRow["ContractID"] = contact.ContractID;
        newRow["PersonName"] = contact.PersonName;
        newRow["ContactDate"] = contact.ContactDate;
        newRow["ContactTime"] = contact.ContactTime;
        newRow["TypeOfContact"] = contact.TypeOfContact;
        newRow["ContactMadeBy"] = contact.ContactMadeBy;
        newRow["ContactDescription"] = contact.ContactDescription;
    }

    MessageBox.Show(dTable.Rows.Count.ToString());

您可以看到导致数字的两个MessageBox,我是否将数据错误地加载到DataTable 中?

【问题讨论】:

  • 新建DataRow后仍需将其添加到DataTable中。
  • 有什么理由不直接填充数据表而不是从对象转换它?此外,对于它的价值,myReader.GetDateTime(6)myReader.GetInt32(0) 可能比您的方法更直接。
  • 此外,如果您只是将数据发送到DataTable,为什么还要使用临时Contact 列表?而且,如果可能的话,我认为Contacts 的集合将是使用数据的更好方式,而不是DataTable..
  • 在将项目列表分配给新行之后,在 foreach 循环中类似于 dTable.Rows.Add(newRow);。此外,正如其他人指出的那样,您正在对数据进行相当多的迭代。除非您特别需要同时使用数据表和联系人列表,否则您实际上应该只保留这两者之一,尤其是考虑到您正在处理一个相当大的集合。
  • 我纯粹是在使用这种方法,因为我在加载到 CSV 时遇到了日期字段的格式问题。它们的格式为 dd-MM-yyyy,我需要它们 yyyy-MM-dd。我相信这是由于 FileHelpers 包造成的,因为我曾尝试以多种方式转换 Date 列,但它只是不起作用。我现在希望[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")] public DateTime ContactDate; 能解决这个问题。

标签: c# list csv datatable


【解决方案1】:

您必须将新行添加到DataTable

foreach (var contact in listOfContacts)
{
    var newRow = dTable.NewRow();
    newRow["ContactID"] = contact.ContactID;
    newRow["CompanyID"] = contact.CompanyID;
    newRow["EmployeeID"] = contact.EmployeeID;
    newRow["PersonID"] = contact.PersonID;
    newRow["ContractID"] = contact.ContractID;
    newRow["PersonName"] = contact.PersonName;
    newRow["ContactDate"] = contact.ContactDate;
    newRow["ContactTime"] = contact.ContactTime;
    newRow["TypeOfContact"] = contact.TypeOfContact;
    newRow["ContactMadeBy"] = contact.ContactMadeBy;
    newRow["ContactDescription"] = contact.ContactDescription;

    dTable.Rows.Add(newRow);  // YOU NEED THIS LINE TO ADD THE NEWROW TO DATATABLE
}

【讨论】:

  • 我更喜欢的另一种方法是使用var newRow = dTable.Rows.Add(); 创建行。然后它此时已经添加了默认值,您可以在之后设置所有字段。
【解决方案2】:

在你的 foreach 循环最后添加这个:

dTable.Rows.Add(newRow);

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 2019-02-28
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2020-03-16
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多