我正在使用这个库:
使用 sqlbulkcopy 将来自 xml 解析的一些 linq 查询结果放入数据库,该数据库使用 datareader 直接放置数据,而不使用数据表或此类数据结构。
我无法实现它。
这是我到目前为止写的代码,没有任何成功:
XDocument xdoc = XDocument.Parse(str_XmlToParse);
//ORDINI
IEnumerable<XDocument> xdod;
var orders = from c in xdoc.Descendants("ordersdata").AsEnumerable().Descendants("order").AsEnumerable()
select new
{
Client_ID = (string)c.Element("Client").Element("ID"),
Doc_ID = (string)c.Element("ord_ID"),
Doc_data = (DateTime)c.Element("ord_datetime"),
};
IDataReader dr = orders.AsDataReader();
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlTransaction tran = con.BeginTransaction())
{
var newOrders =
from i in Enumerable.Range(0, totalToSend)
select new Order
{
customer_name = "Customer " + i % 100,
quantity = i % 9,
order_id = i,
order_entry_date = DateTime.Now
};
SqlBulkCopy bc = new SqlBulkCopy(con,
SqlBulkCopyOptions.CheckConstraints |
SqlBulkCopyOptions.FireTriggers |
SqlBulkCopyOptions.KeepNulls, tran);
bc.BatchSize = 1000;
bc.DestinationTableName = "order_queue";
bc.WriteToServer(newOrders.AsDataReader());
tran.Commit();
}
con.Close();
这主要是该库示例中内容的副本。
我什至没有正确理解他们所有奇怪的铸件是怎么回事,但我需要它工作。 (相信我,我试着去理解)
我一直收到这个错误:
Error 1 'System.Collections.Generic.IEnumerable AnonymousType#1>' does not contain a definition for 'AsDataReader' and no extension method 'AsDataReader' accepting a first argument of type 'System.Collections.Generic.IEnumerable AnonymousType#1>' could be found (are you missing a using directive or an assembly reference?)
这是什么?我该如何解决?
谢谢
最佳答案
嗯,基本上错误是说你不能应用参数为“IEnumerable”对象的方法“AsDataReader”,因为这个定义没有这样的方法。如果可能有帮助,请尝试在引用中添加 DLL“System.Data.Entity”和 System.Data.EntityClient
关于c# - 来自 LINQ Query for SqlBulkCopy 的 Datareader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270238/