I have serialized a database values to XML and kept it inside the Apps debug folder. Now i want to deserialize it and insert to SQL database.
Here is what i did:
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);
}
}
The class contains :
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);
}
}
The onButton click to save the file:
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)};
Thus; DocData = contents;
Now its showing the error: The given value of type String from the data source cannot be converted to type varbinary of the specified target column.