1

我有一个班级客户。我正在尝试从 access db 数据库加载数据。

客户类结构如下:

public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
        public string CustAddress { get; set; }
        public string PnoneNo { get; set; }
        public string MobileNo { get; set; }
        public string CstNo { get; set; }
        public string DlNo { get; set; }
        public decimal BalAmt { get; set; }
            }

我在db中的表结构如下:

在此处输入图像描述

现在,当我尝试在客户类中加载数据时,它会引发错误:

“指定的演员表无效。”

为了将数据加载到类中,我使用以下代码:

public static List<Customer> LoadListItems(string strTable, string strOrderBy)
        {
            List<Customer> lstCustomer=null;
            try
            {
                DataUtility objDataUtility = new DataUtility();
                DataTable objCustomerList = objDataUtility.LoadCustomerInfo(strTable, strOrderBy);

                lstCustomer= objCustomerList.AsEnumerable().Select(row =>
                        new Customer
                        {
                            CustomerId = row.Field<int>("CID"), //throwing error for this line
                            CustomerName = row.Field<string>("salPNm"),
                            CustAddress = row.Field<string>("cadd"),
                            MobileNo = row.Field<string>("cmbl"),
                            PnoneNo = row.Field<string>("cph"),
                            DlNo = row.Field<string>("cDlN"),
                            CstNo = row.Field<string>("cTin"),
                            BalAmt = row.Field<decimal>("cobal")
                        }).ToList();


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstCustomer;

        }

在上面的方法CustomerId = row.Field<int>("CID"),中,当我评论这一行时,它会引发错误,因为它工作正常。请帮助我如何从 iennumrable 列表中获取 int 值。

提前致谢。埃什维尔

4

2 回答 2

0

尝试这个

public class Customer
{
    public Int64 CustomerId { get; set; }
    public string CustomerName { get; set; }
    public string CustAddress { get; set; }
    public string PnoneNo { get; set; }
    public string MobileNo { get; set; }
    public string CstNo { get; set; }
    public string DlNo { get; set; }
    public decimal BalAmt { get; set; }
}

CustomerId = row.Field<Int64>("CID")

我认为你的身份是一个长整数。

于 2012-10-14T17:50:09.270 回答
0

将其替换为 -

CustomerId = Convert.ToInt64(row.Field<int>("CID"));

此外,通过Quick Watch在此行上应用来检查值 - row.Field<int>("CID")。看看它是否not null以及它返回的值是多少。

于 2012-10-14T17:38:15.307 回答