1

Which datatype should be used in MSSQL database with an MVC application for the following fields as on the following components? http://demos.telerik.com/aspnet-mvc/editor/index

  • Editor & File
  • Image browser
Michael
  • 32,527
  • 49
  • 210
  • 370
Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    Looking at the code the image is stored as a file on disc. For HTML content I would say varchar(max). – Sean Lange May 05 '15 at 14:30
  • Thanks for reply. I use byte[] for ImageData, but I am not sure if I can also use byte[] for the other file types. On the other hand, could you please clarify me on how to store these files to the table by just matching the content types with data type in MSSQL (or MVC data type) as this: HTML Content >>> String Image >>> byte[] Other file Types >>> byte[] – Jack May 05 '15 at 14:51
  • On the other hand, I think I will store each file in a new row of the table, is that true? Or is there any better way i.e. storing all of them to one row by combaining? – Jack May 05 '15 at 14:52
  • There is no such thing as a byte[] in sql server. That is your dotnet datatype. Those should be stored in a varbinary(max) column in sql server. String is also not a datatype in sql server. Again that is your dotnet datatype. That would be (n)varchar([int] or [max]) in sql server. – Sean Lange May 05 '15 at 14:57
  • Sorry, as I use Entity Framework Code First, I meant .NET data Types. Could you please clarify me about the data types of the following fields? Thanks. HTML Content >>> ? --- Image >>> ? Other file Types >>> ? – Jack May 06 '15 at 06:31
  • 1
    From the dotnet side, html would be a string. ANY kind of file would be a byte[]. – Sean Lange May 06 '15 at 13:13

1 Answers1

0

In my application I sotre image as base64string hence, nvarchar(max) is the data type i used. The advangage of this is that you can pass based64String image in JSON object from or to website.

To convert image to based64string

public static string ToBased64String(this Image image, ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();
    string based64String = Convert.ToBased64String(imageBytes);
    return based64String;

  }
}

then you can call your method like this

image.ToBased64String

To convert based64String to image

public static ImageFromBased64String(string based64Image, string path)
{
  Image image = null;
  var bytes = Convert.FromBased64String(based64String);
  using (var fileStream = new FileStream(path, FileMode.Create))
  {
    fileStream.Write(bytes, 0, bytes.Length);
    fileStream.Flush();
    image = Image.FromStream(fileStream, true);
    return image;
  }
}
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
  • Thanks for reply, actually it seems to be logical. What about the HTML Content (Editor data) and Other file Types (rar, pdf, word, zip, etc.)? – Jack May 06 '15 at 06:32