【发布时间】:2014-11-26 22:11:23
【问题描述】:
我正在尝试使用 sqlBulkCopy 将数据插入到数据库表中,它工作正常 数据被插入,然后我想查询我的数据,但是当我引用更新的表时,我收到错误:对象为空。我可以使用实体结构查看此表,但此对象的值为 null:
using (var cn = new AdventureWorksEntities())
{
cn.Database.ExecuteSqlCommand("TRUNCATE TABLE tmpTable");
sqlConnectionString = cn.Database.Connection.ConnectionString;
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "dbo.tmpTable";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(table);
}
catch (Exception ex)
{
//ErrorHandler
}
}
// Now I am trying to get data from the updated table:
var uniqueItemIDs = cn.tmpTable.Select(m => m.ItemID).Distinct();
// And from the line above I am getting the Error: tmpTable is null.
}
其他表(未使用 SqlBulkCopy 更新的)正常且可访问。有人可以帮忙吗?
【问题讨论】:
-
你可以访问
cn.tmpTablebefore SqlBulkCopy 的东西吗?我认为该表不属于您的 EF(?) 数据上下文。 -
是的,我可以。我也可以从 edmx 图中看到。
标签: c# sql .net sqlbulkcopy