2016-11-24 47 views
0

我已經將數據庫值序列化爲XML並將其保存在Apps調試文件夾中。現在我想反序列化它並插入到SQL數據庫。使用LinqToEntities將XML字符串轉換爲Varbinary(MAX)

這裏是我做的:

XmlDocument XDoc = new XmlDocument(); 

      XDoc.Load("Serialized.xml"); 
      var xmlcontents = XDoc.InnerXml; //All XML Contents 
      DataSet ds = new DataSet(); 
      StringReader sr = new StringReader(xmlcontents); 
      ds.ReadXml(sr); 
      DataTable offlineDT = ds.Tables[0]; 
      var context = new SysventLinqClassDataContext(); 


      context.Connection.Open(); 
      //The transaction Part 
      using (context.Transaction = context.Connection.BeginTransaction()) 
      { 
       using (var bulkCopy = new SqlBulkCopy(context.Connection.ConnectionString, SqlBulkCopyOptions.KeepIdentity)) 
       { 
        bulkCopy.ColumnMappings.Add("DocTitle", "DocTitle"); 
        bulkCopy.ColumnMappings.Add("DateOfReceipt", "DateOfReceipt"); 
        bulkCopy.ColumnMappings.Add("Descriptions", "Descriptions"); 
        bulkCopy.ColumnMappings.Add("HouseID", "HouseID"); 
        bulkCopy.ColumnMappings.Add("DocData", "DocData".ToString()); 
        bulkCopy.ColumnMappings.Add("SiteID", "SiteID"); 
        bulkCopy.ColumnMappings.Add("EmployeeID", "EmployeeID"); 


        bulkCopy.BulkCopyTimeout = 600; 
        bulkCopy.DestinationTableName = "tblDocument"; 
        bulkCopy.WriteToServer(offlineDT); 
       } 

      } 

類包含:

public string DocTitle { get; set; } 
     public string DateOfReceipt { get; set; }  
     public string Descriptions { get; set; } 

     [System.Xml.Serialization.XmlElementAttribute("DocData", DataType ="base64Binary")] 
     public byte[] DocData { get; set; } //This is a PDF document as varbinary(MAX) in mySQL Database 
     public int SiteID { get; set; } 
     public int HouseID { get; set; } 
     public int DesignID { get; set; } 
     public int EmployeeID { get; set; } 


     public void Save(string filename) //TO be called inside the main 
     { 
      using (var stream = new FileStream(filename, FileMode.Create)) 
      { 
       var xmlData = new XmlSerializer(typeof(Class1)); 
       xmlData.Serialize(stream, this); 
      } 
     } 

的onButton點擊保存文件:

FileStream fStream = File.OpenRead(filepath); 
       byte[] contents = new byte[fStream.Length]; 
       fStream.Read(contents, 0, (int)fStream.Length); 
       fStream.Close(); 


       Byte[] hexByte = new Byte[2]{Convert.ToByte(100), 
       Convert.ToByte(50)}; 

因此, DocData =內容;

現在顯示錯誤:來自數據源的String類型的給定值無法轉換爲指定目標列的varbinary類型。

+0

有什麼問題嗎?還是錯誤?你顯示的代碼你到目前爲止(太棒了!),但我沒有看到你的文章中的任何東西似乎是一個問題或我們應該幫助你。 –

+0

@JonathanMagnan錯誤是'來自數據源的String類型的給定值無法轉換爲指定目標列的varbinary類型' – DevRingim

回答

1

The error is 'The given value of type String from the data source cannot be converted to type varbinary of the specified target column

這是一個非常常見的錯誤。對於像varbinary這樣的一些類型,您必須指定DataColumn的列類型,否則將被視爲字符串並且無法轉換。

但是,一旦DataTable填充了數據,就不能更改列類型。一個解決辦法是克隆表和導入數據

(從AKHIL here原來的答案)

DataTable dtCloned = dt.Clone(); 
dtCloned.Columns[0].DataType = typeof(byte[]); 
foreach (DataRow row in dt.Rows) 
{ 
    dtCloned.ImportRow(row); 
}