c# - 来自 LINQ Query for SqlBulkCopy 的 Datareader

标签 c# linq anonymous-types sqlbulkcopy datareader

我正在使用这个库:

Link to EntityDataReader

使用 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/

相关文章:

asp.net-mvc - MVC View 中的动态类型

c# - 是否可以更改值类型变量的默认值?

c# - 一旦知道字典的大小是固定的,有没有办法削减它的容量?

c# - SharePoint Web 服务的 LINQ to XML 问题

c# - Linq-to-SQL 无法反射(reflect)一个存储过程中的更改

c# - 直到运行时才知道属性的匿名类型

c# - 如何遍历匿名类型的列表

c# - 在 Windows Phone 8.1 运行时获取音乐文件太慢

c# - 为什么选择 SharpDevelop 而不是 Visual Studio 来使用 C# 进行编码?

c# - 在 Select 和 Where 调用中重用 Linq to Entities 的 Expression<Func<T, TResult>