2013-06-24 86 views
2

我使用這個庫:的DataReader的SqlBulkCopy的

Link to EntityDataReader

把從XML一些LINQ查詢結果解析到一個數據庫與我們上課一個DataReader把數據使用SqlBulkCopy直接不使用數據表或這種數據結構。

我無法執行它。

這是我至今寫了沒有任何成功的代碼:

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?)

這是什麼?我如何解決它?

感謝

+0

PS。我認爲這是一個編譯器錯誤,而不是一個例外。正確? – Aron

+2

你好。我非常渴望得到這個圖書館。我跟着鏈接,但它已經退休了。任何人都知道我可以在哪裏下載它? – serlingpa

+1

@serlingpa 也許爲時已晚,但.... https://code.msdn.microsoft.com/windowsdesktop/WPF-783fc64b/sourcecode?fileId=94546&pathId=1773531594 –

回答

2

好了,基本上錯誤說,因爲沒有這個定義這樣的方法,你可以不帶參數應用方法「AsDataReader」的「IEnumerable的」對象。如果可能有所幫助,請嘗試在引用中添加DLL「System.Data.Entity」和System.Data.EntityClient

+0

試過了,但我沒有期待它的工作。這只是一個從匿名類型到可枚舉列表或其他東西的錯誤輸入問題。 –

+0

什麼類型是「訂單」變量?因爲我猜想,你的LINQ查詢的結果不會返回你可以應用「AsDataReader()」方法的東西。順便說一句:爲什麼你把2「.AsEnumerable()」?我想你只需要一個 – davideberdin

+0

剛看了一下你添加的樣本。您需要在頂部使用Microsoft.Samples.EntityDataReader'(除非您已更改爲「EntityDataReader.cs」文件的源代碼)。原因是您需要讓編譯器查找「Microsoft.Samples.EntityDataReader.EntityDataReaderExtensions」靜態類。 – Aron