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類型。
有什麼問題嗎?還是錯誤?你顯示的代碼你到目前爲止(太棒了!),但我沒有看到你的文章中的任何東西似乎是一個問題或我們應該幫助你。 –
@JonathanMagnan錯誤是'來自數據源的String類型的給定值無法轉換爲指定目標列的varbinary類型' – DevRingim