LINQ:读取Excel文件,然后写入数据库

时间:2011-05-23 19:57:39

标签: c# linq linq-to-sql linq-to-excel

我使用LinqToExcel从Excel文件中获取行,然后使用LINQToSQL写入SQLServer。它DID在数据库中创建记录但具有空值。我想知道是否需要将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; }

1 个答案:

答案 0 :(得分:1)

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

你应该从x以某种方式初始化它。类似的东西:

HSD_TELE_INSTALL myRecord = new HSD_TELE_INSTALL(){SomeField=x.SomeField /*etc...*/ };
db.HSD_TELE_INSTALLs.InsertOnSubmit(myRecord);
db.SubmitChanges();