0

I am trying to import some records from Access to SQL server using LINQ to SQL, and I received the error in the subject at "select new tblName" (select is underlined) in the following code:

        OdbcConnection myconnection = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + this.txtFullPath.Text);
        OdbcDataAdapter myadapter = new OdbcDataAdapter("SELECT * FROM Name", myconnection);
        DataSet myCustomersDS = new DataSet();
        myadapter.Fill(myCustomersDS, "Name");

        //LINQ
        iMISDataContext db = new iMISDataContext();

        // inserting multiple items
        tblName toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            select new tblName
            {
                ID = row.Field<string>("ID"),
                ORG_CODE = row.Field<string>("ORG_CODE"),
                MEMBER_TYPE = row.Field<string>("MEMBER_TYPE"),
                //... and rest of the columns (lots of columns)
            };
        db.tblNames.InsertAllOnSubmit(toInsert);
        db.SubmitChanges();

There is another error also at the 2nd last line: The type arguments for method 'System.Data.Linq.Table.InsertAllOnSubmit(System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Thank you, Steven

4

1 回答 1

1

当您查询对行进行投影时,myCustomersDS.Tables["Name"]实际上是在选择一些对象而不是单个对象。所以改变

// inserting multiple items
        tblName toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            .......

至:

// inserting multiple items
        var toInsert =
            from row in myCustomersDS.Tables["Name"].AsEnumerable()
            .....
于 2013-11-06T18:39:49.127 回答