How to update Entity Framework from DataSet that is reading XML? - c#

I want to update my database over a WCF Data Service and the Entity Framework
from an XML file read from disk.
I am using a DataSet to read the XML file but I don't know how to update the database from the dataset via the Entity Framework part.
I am using a DataSet to read the XML because it works so well but if there
is another way to read XML into the database via Entity Framework that would be great.
The code I want looks like the following.
private void AddToDBFromXMLFile(string xmlFile)
{
// this is reading my XML just fine, but i suspect there is another way to
// get the XML into the
// database, otherwise how can I get the XML into the database via the EF?
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);
// this is working and can update and retrieve my data using the usual methods
var proxy = new TestApp.ServiceReference.TestEntities(new
Uri("http://localhost:4976/TestWcfDataService.svc/"));
// how do I update the database from the dataset?
//proxy << dataset
}

Related

How to add CSV reader "records" to database?

I am currently building a .NET service that takes a CSV file, parses it, and stores the parsed data into the database. I've already built the parse logic using CSVhelper. I am fairly new to Backend languages and cannot figure out how to store the data into the database. There isn't a lot of code, but I know i'm making a mistake somewhere.
Parse Service code:
public void ReadMonitoredEvent( IFormFile file)
{
MonitoredEvent monitoredEvent = new MonitoredEvent();
TextReader reader = new StreamReader(file.OpenReadStream());
var csvReader = new CsvReader(reader);
var records = csvReader.GetRecords<MonitoredEvent>();
this.dbContext.MonitoredEvents.Add(monitoredEvent);
this.dbContext.SaveChanges();
}
I've tried changing out the parameters for the "this.dbContext.MonitoredEvent...." but can't seem to find the proper entity to put as parameters
I tested in Postman without the dbContext part and I get a successful 200 code returned, but with the dbContext statements I am getting:
"NullReferenceException: Object reference not set to an instance of an
object."
at line: this.dbContext.MonitoredEvents.Add(monitoredEvent);
If someone can help me get this sorted out so I can store it into my database I'd appreciate it. I already have my entity model built and all the headers in the CSV match up.

storing dataset in datagridview to a new access db file c#

Actually I have some data loaded from a access db file in datagridview (in my winform application) and after making changes on dataset I want to save it in a new table of a new access db file through save file dialogue.
I've searched about this but in most of articles they usually tell about updating the dataset in the same access file from which the data has been loaded.but I want the application to create a new access db file with updated data in it. is it possible to do so?
and yup I'm using Oledb technology for accessing data.
thank you
Make a copy from exist db (File.copy), change the connection string and save the changes in this new database.
public string _strCnn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\dataBase.accdb;";
public void SaveChanges()
{
System.IO.File.Copy(#"C:\myFolder\dataBase.accdb;", #"C:\myFolder\NEWdataBase.accdb;");
_strCnn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\NEWdataBase.accdb;";
//TODO: Save changes
}

Import large XML file into SQL Server CE

I try to import data from a XML file into SQL Server CE database. I use ErikEJ SQL Server Compact Bulk Insert Library (from NuGet) this library on codeplex. I create database and table. Then I read XML to DataTable and import this DataTable to DB table.
DataSet ds = new DataSet();
ds.ReadXml("myxml.xml");
DataTable table = new DataTable();
table = ds.Tables[0];
String connString = #"Data Source = test.sdf";
SqlCeBulkCopy bulkInsert = new SqlCeBulkCopy(connString);
bulkInsert.DestinationTableName = "testtable";
bulkInsert.WriteToServer(table);
It works on a small xml, but when I use large xml (more then 1gb) I get this error on ReadXml :
"System.OutOfMemoryException" in mscorlib.dll
How to fix this?
update: I know that this error because I use large xml - question is how optimize this algorithm, mayby using buffer or read xml part by part, any idea?
There is no simple libary that will solve this for you.
You need to read the XML file in a streaming fashion ( Reading Xml with XmlReader in C# ) to avoid loading the entire XML file, and then for each element read add these to a List or DataTable, up to say 100,000 entries, then BulkInsert those, dispose/clear all unused objects and go on, until the entire file has been read.
In addition, calls to SqlCeBulkCopy should be wrapped in usings to dispose unmanaged resources:
using (SqlCeBulkCopy bulkInsert = new SqlCeBulkCopy(connString))
{
bulkInsert.DestinationTableName = "testtable";
bulkInsert.WriteToServer(table);
}

Exporting data from database to text file

I have a table in my database called Employeee.
I need to write the database data to a file.
How do I do it in asp.net mvc C#?
i have tried Datatable approach,but it failed.
Do It the same way you would do in a winform, wpf or so.
For example, if your text document has to be in JSON, use :
using(TextWriter writer = new TextWriter(#"yourfilename.json")){
writer.Write(JsonConvert.SerializeObject(db.Employeee));
}
In my example, don't forget to import the Newtownsoft.Json package.
You can also formate everything in XML, CSV...

how to pass xml data to gridview?

Recently i am stuck with a problem during my development.I have some xml values in a variable (not in a seperate file).I transform the xml using xslt and displayed them using xml control in my webpage.NOw my requirement has been changed and I supposed to display the xml values in Gridview instead of xml control.
string getval= //some stuff where webservice returns xml.
// here I specified content to xmldynamic control
xmldynamic.TransformSource=//my xslt comes here..
Now I want to display the transformed XML in gridview.Is it possible...If possible how..
I surfed through net and identified that if xml is in a file, then xml data source can be used,transformed and define datasource to gridview.But in my case its in a variable.
There a lot of 3rd party Class / Software that does that.
For example for web an this is a powerfull app maybe you dont want this extensive app on your side but consider ExtJS
http://docs.sencha.com/extjs/4.2.2/
///
Also check this old topic for more
Is there an XML Editor with grid view similar to that of XMLSpy?
Try below steps:
1.) Load the XML data in a StringReader
2.) feed this StringReader object to ReadXml() method of DataSet
3.) Now bind this DataSet to your GridView.
This step is always tricky ( May be) as depending on the XML format Many times I needed to modify it slightly as : ds.Tables[0]; or ds.Tables[1]; etc...
Sample:
string xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<Cameras>
<Camera>
<Model>Canon EOS-1D</Model>
<Price>$5219</Price>
</Camera>
<Camera>
<Model>Canon EOS-1D Mark IV</Model>
<Price>$5000</Price>
</Camera>
</Cameras>";
StringReader sr = new StringReader(xml); // Step 1
DataSet ds = new DataSet();
ds.ReadXml(sr); // Step 2
GridView1.DataSource = ds.Tables[0]; // Step 3
GridView1.DataBind();

Categories