0

我正在使用 LinqToExcel 从 Excel 文件中获取行,然后使用 LINQToSQL 写入 SQLServer。它确实在数据库中创建了记录,但具有空值。我想知道是否需要将 Mac 对象(linqtoexcel)映射到 HSD_TELE_INSTALLs 对象?我不认为我离它太远了,因为它插入了记录。相关代码如下。提前致谢。

        //LINQ
        var macs = from x in excel.Worksheet<Mac>(sheet)
                   select x;
        //ITERATE WITH LINQ RESULTS
        foreach (var x in macs)
        {
            HSD_TELE_INSTALL myRecord = new HSD_TELE_INSTALL();
            db.HSD_TELE_INSTALLs.InsertOnSubmit(myRecord);
            db.SubmitChanges();
        }

    public class Mac
    {
        public string REGION { get; set; }
        public string MACID { get; set; }
        public string HOUSEKEY { get; set; }
        public string HOUSENUM { get; set; }
        public string STREET { get; set; }
        public string UNIT { get; set; }
        public string ADDRESS2 { get; set; }
        public string COMMUNITY { get; set; }
        public string STATE { get; set; }
        public string ZIPCODE { get; set; }
        public string TECHNICIAN { get; set; }
        public string JOBNO { get; set; }
        public string JOBTYPE { get; set; }
        public string CLOSEDATE { get; set; }
        public string CLOSETIME { get; set; }
        public string COMMENTS { get; set; }
        public string MGT { get; set; }
        public string COMPLETIONCODE { get; set; }
        public string TCRSN { get; set; }
4

1 回答 1

1

您正在创建一个新的未初始化类型对象,HSD_TELE_INSTALL然后将其提交给 DB。

您应该以某种方式从x. 就像是:

HSD_TELE_INSTALL myRecord = new HSD_TELE_INSTALL(){SomeField=x.SomeField /*etc...*/ };
db.HSD_TELE_INSTALLs.InsertOnSubmit(myRecord);
db.SubmitChanges();
于 2011-05-23T20:05:18.367 回答