an entire DataTable object to byte then saved in database - c#

im tasked to create a virtual database that clients can create from our web application and have them save data to.
the problem now is to have it stored for later re-use.
im thinking of dynamically creating a DataTable object in c# then convert it to byte[]. now i want to know if this would be practical to save on a database...
is this possible?

You can use WriteXml to write to a stream:
byte[] raw;
using(MemoryStream ms = new MemoryStream()) {
table.WriteXml(ms);
raw = ms.ToArray();
}
...
raw = ...
using(MemoryStream ms = new MemoryStream(raw)) {
table.ReadXml(ms);
}
And write the byte[] to a varbinary(max) / image / etc column in a database if that is what you need (you mentioned database) - or just use xml.
If you are writing to a file, then just use a FileStream in place of a MemoryStream.

Related

Convert base64 string to jpg/file

I am struggling to find a way to convert a base 64 string to a jpeg or a file without using the Image class. Is it possible to create the file and not save it locally and upload to Azure blob storage?
var bytes = Convert.FromBase64String(base64String);
To keep it completely clean and simple, use something like this:
using (var img = new MemoryStream(bytes))
{
cloudBlockBlob.UploadFromStream(img);
}
This creates a MemoryStream that you can use to call CloudBlockBlob.UploadFromStream().
Edit
Or, like #mike-z said in the comment below, you can use CloudBlockBlob.UploadFromByteArray() directly.

Saving File from web to Database c#

Im making this website,where the user has a form in which they can choose a file from their computer,and upload it, but i don't know how i can use it. I'm using MVC and a Web Service also in C# where im handling the connection to the Database,where I have to save the file.The file can be a pdf,word or an image.
So the question is,how can I save it and also check its size.Thank you
You can use the ContentLength property of HttpPostedFileBase to get the size of the file -
https://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.contentlength(v=vs.110).aspx
Then save the InputStream to a byte array
using (MemoryStream stream = new MemoryStream())
{
file.InputStream.CopyTo(stream);
bytes = stream.ToArray();
}
Next assign the byte array to a SqlDbType.VarBinary stored procedure parameter to save it back to the database - assuming this is SQL Server.

byte array to pdf

I am trying to convert content of a file stored in a sql column to a pdf.
I use the following piece of code:
byte[] bytes;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, fileContent);
bytes = ms.ToArray();
System.IO.File.WriteAllBytes("hello.pdf", bytes);
The pdf generated is corrupt in the sense that when I open the pdf in notepad++, I see some junk header (which is same irrespective of the fileContent). The junk header is NUL SOH NUL NUL NUL ....
You shouldn't be using the BinaryFormatter for this - that's for serializing .Net types to a binary file so they can be read back again as .Net types.
If it's stored in the database, hopefully, as a varbinary - then all you need to do is get the byte array from that (that will depend on your data access technology - EF and Linq to Sql, for example, will create a mapping that makes it trivial to get a byte array) and then write it to the file as you do in your last line of code.
With any luck - I'm hoping that fileContent here is the byte array? In which case you can just do
System.IO.File.WriteAllBytes("hello.pdf", fileContent);
Usually this happens if something is wrong with the byte array.
File.WriteAllBytes("filename.PDF", Byte[]);
This creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
Asynchronous implementation of this is also available.
public static System.Threading.Tasks.Task WriteAllBytesAsync
(string path, byte[] bytes, System.Threading.CancellationToken cancellationToken = null);

How to store an image in a SQL Server database from Silverlight app?

I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:
How to convert an image to binary from a url to store it into my database (store all the image and not only the url)
Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)
Finally, when the image is stored, how to read it from Silverlight?
Thank you in advance .
You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.
System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;
// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)
To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).
Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/
Add bytes from memory stream into database
Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx
It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

Compress picture and photos to store in SQL Server database

I want to save many pictures into a SQL Server database file. How can I compress pictures to store in database? What technologies can I use?
Please help me
After Edit:
How Can I Compress And Resize Pictures to Store in Sql Server DataBase?
Pictures don't compress much if they already in jpg and most other formats. You could try compressing them further before sending to SQL Server but it probably isn't worth the effort, unless you have BMPs.
I suggest you look at FILESTREAM to store them in SQL Server (assuming SQL Server 2008) which is more efficient for this kind of data.
You could save them in JPEG and include the file. For doing it, you can use Image.Save method. And if you want to set the quality you can use Encoder.Compression.
You should also make sure that the resolution of the images is not too much.
You can store pictures in sql database by convert images to byte array.You can do it with below code :
byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}

Categories