I need to read blob data into a memory stream from an Image type column from an SQL Server database. How can I do this using Dapper?
I was reading the Dapper manual but was unable to find information about this.
UPDATE: I need to read the data from the database (from a query). All the links suggested so far has information about how to store the blob in the database.
Figured it out. The result dynamic type is a byte[].
var row = con.QueryFirst("SELECT BLOBFIELD FROM TABLE WHERE ID = 1");
byte[] bytes = drawings.BLOBFIELD;
using (var stream = new System.IO.FileStream(#"C:\Temp\Test.dat", System.IO.FileMode.CreateNew))
stream.Write(bytes, 0, bytes.Length);
I have an App which communicate to a MySQL database via webservice. The webservice serves the app with XML. Now I want to replace the MySQL database with a SQLite database. To avoid changing all logic I only need to get an XML format back from my SQLite database. To increase the level of problem, I have to read data from more than one table. Details of App: For each table I have the structure of my tables in class stored and this is the code I use currently which is not working:
XmlSerializer xs = new XmlSerializer(typeof(myTableClass));
var sRe = new myTableClass();
using (XmlWriter writer = XmlWriter.Create(sww))
{
xs.Serialize(writer, sRe);
var buf = sww.ToString();
return buf;
}
I expect a string of my XML in the variable buf, but when I run this code it just jump out on the first row. What is wrong in my code?
First of all, I'm very new to C# and Json.
I wanted to plot a graph from mysql table data in a C# GUI. I made a PHP file to select the data from mysql database table and echoed the selected contents in json array using echo json_encode(array("result"=>$result))
here is my PHP:
<?php
define('HOST','*********************');
define('USER','*********************');
define('PASS','*********************');
define('DB','***********************');
if($_SERVER['REQUEST_METHOD']=='GET'){
$start = $_GET['start'];
$ending = $_GET['ending'];
}
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT * FROM table WHERE date_time BETWEEN '$start' and '$ending'" ;
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'p_pairs'=>$row[1],'temp1'=>$row[2] ,'temp2'=>$row[3],'temp3'=>$row[4],'temp4'=>$row[5],'temp5'=>$row[6],'avg_current'=>$row[7],'avg_voltage'=>$row[8],'kw'=>$row[9],'kwh'=>$row[10]));
}
echo json_encode(array($result));
mysqli_close($con);
?>
using the link, I can see the json array clearly between the datetime gap.All I wanted is to plot the graph of temperature values (temp1, temp2,..), p-pairs values and others with the date_time in a line graph to read the historical data.
All I get when I access the PHP page is:
[[{"id":"1","p_pairs":"0000-00-00 00:00:00","temp1":"2","temp2":"100","temp3":"100","temp4":"100","temp5":"100","avg_current":"100","avg_voltage":"300","kw":"300","kwh":"300"},{"id":"2","p_pairs":"0000-00-00 00:00:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"},{"id":"3","p_pairs":"2016-01-07 21:10:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"},{"id":"4","p_pairs":"2016-01-07 21:10:00","temp1":"45","temp2":"105","temp3":"230","temp4":"100","temp5":"2500","avg_current":"570","avg_voltage":"100","kw":"250","kwh":"1000"}]]
NOTE: some datetime is set as default here. I just wanted to show this array. The correct one will be perfect to plot the graph.
I can can take these array in a string using a web request from C#. using the bellow code:
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("url to php");
string webData = System.Text.Encoding.UTF8.GetString(raw);
It'll be a big help If someone can help me to plot this in a line graph as date_time Vs temp1, temp2 or p_pairs and like that in C# GUI...
This is going to be a big help for me.
Thanking you in advance.
Firstly get a class to work from. You can dynamically create a class from Json
If you are happy to have a dependency upon the System.Web.Helpers assembly, then you can use the Json class:
dynamic data = Json.Decode(json);
It is included with the MVC framework as an additional download to the .NET 4 framework. Or use the NewtonSoft one.
Next get a chart component: https://msdn.microsoft.com/en-us/library/dd489237.aspx and bind your data to the chart
I have a webapp that needs to import 3 different remote XML files every night. These files have student and parent information, study information, etc.
Can someone point me to information how to read the XML (via http) and then loop over every student so I can add information from the other XML files and store the relations into my database?
Need some example code like:
Open & read Parents XML and store in database with fixed Ids from XML file
Open & read Students XML and store in database AND link to their respective parents using the parent id's from the parent XML
Also store study information for every student
I need to know what my strategy would be. What would the smartest and most efficient method be to accomplish this using Entity Framework?
I don't see how you would use the Entity Framework directly with your XML files. EF is designed to work with relational databases.
I think you will have to import the data into a relational database in some way.
If using SQL Server, you can achieve such an import (given you have downloaded the XML file already with the help of a scheduled task on the web server) using a DataSet and SqlBulkCopy:
// Create DataSet and load data
DataSet ParentData = new DataSet();
ParentData.ReadXml(Server.MapPath("ParentFile.xml"));
// Create SqlBulkCopy object
SqlConnection connection = new SqlConnection("YourConnectionString");
SqlBulkCopy bulkCopy = new SqlBulkCopy(connection);
bulkCopy.DestinationTableName = "YourParentTable";
// Get DataTable and copy it to database
DataTable ParentTable = ParentData.Tables["Parent"];
bulkCopy.WriteToServer(ParentTable);
First a bit of advice..
There is a caveat when you are using only a webapp, because it won't automatically fetch the updated files. At least not out-of-the-box. A webapp will be unloaded by IIS when it's not used for a long time, and is almost always a "reactive" application.
But... it is possible to schedule a task in which you open a webpage at a certain time each night, and start the import that way.
You could use the System.Net.HttpWebRequest to fetch the xml's to a local temp folder like this:
HttpWebRequest req = WebRequest.Create("http://url.to/file.xml") as HttpWebRequest;
// check if the cast went well
if (req != null) {
try {
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
System.IO.FileStream outFileStream =
System.IO.File.Create(#"Path\To\localfile.xml");
resp.GetResponseStream().CopyTo(outFileStream);
outFileStream.Close();
outFileStream.Dispose();
}
catch (ExceptionTypeA ex1) {
// Catch all specific exceptions... ommitted here for brevity
}
}
You can then use XDocument / XElement ( Linq to Xml ) to extract data from the Xml files and use standard ADO.NET API to create and process the DB Import.
Only after the import into a (relational) database you can fully use the entity framework to do the data retrieval / modification. You might even be able to convert all xml data into structured collections of POCO types and use the entity framework to query these collections, but I honestly don't think that's the best way to do it.
My Class in C# which should be send to PHP:
MyData data = new MyData();
data.Name = "MyName";
data.Type = "MyType";
data.ID = 1;
data.Description = "MyLongDescription...";
How do I send it to PHP via C# in JSON format? And how do I retrieve it in PHP?
The next step should be to insert the submitted data/values into a MySQL DB.
C# -> Send some JSON Data to PHP -> Write JSON Data to MYSQL DB via PHP
Sample Database:
MyData(varchar Name, varchar Type, int ID, varchar Description)
My current PHP Code:
$json = json_decode($input,true);
$connection = mysql_connect("localhost","root","");
if (!$connection)
{
die(mysql_error());
}
mysql_select_db("MyDataDB", $con);
mysql_query("INSERT INTO MyData (Name, Type, OtherID, Description)
VALUES('$json[Name]','$json[Type]','$json[ID]','$json[Description]')");
Any ideas?
Thanks for your efforts.
C# code to encode:
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(yourCustomObject);
PHP code to decode:
$decoded = json_decode($received_json_string);
What have you tried, by the way?
I'll answer this, but to Quentin's point, you've got a lot going on.
There is no direct interop between C# and PHP so you'd need to expose a web service from PHP and consume it in your C#. Better yet, have the C# save directly to mysql and don't worry about interop.