我正在尝试使用LINQ to SQL将一些记录从Access导入到SQL服务器,并且我在以下代码中的“select new tblName”(select is underlined)中收到了主题中的错误:
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();
最后一行还有另一个错误: 无法从用法中推断出方法'System.Data.Linq.Table.InsertAllOnSubmit(System.Collections.Generic.IEnumerable)'的类型参数。尝试明确指定类型参数。
谢谢你, 史蒂芬
答案 0 :(得分: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()
.....