0

Here is my problem: I need to store HTML in a MySQL database. Afterwards, I need to be able to retrieve the HTML and have it be valid HTML that a browser can render.

My question: How can I store HTML in a MySQL database using .Net? How do I retrieve it afterwards? As this is the design phase, I can create the database any way that is needed. Thank you.

P.S. I have seen some posts on this using PHP and JAva but I am not using PHP or Java and those posts did not really answer my question.

3 Answers 3

1

You can store HTML as a binary stream in MySQL's BLOB format. Also you can retrieve it in .NET.

To retrieve and store it, you can simply use Byte[] in .NET.

This is a sample using BLOB: http://msdn.microsoft.com/en-us/library/87z0hy49.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

I knew of this method but thought there would be a better way; since no one came up with a better solution and this is in fact the way I am doing it now, I am accepting this answer.
1

First of all, do want want to store a file or just the html text? Since you say php, I take it that u are using web based. If so, I suggest storing the html text as string.

First of read the content of the html file using something like this:

using( StreamReader reader = new StreamReader( @"c:\index.html" ) )
{
 String line = String.Emtpy;
 while( (line = reader.ReadLine()) != null )
 {
  Console.WriteLine( line );
 }
}

Then, just do a normal insertion of string in a reasonable string data type of MySQL. (Cant recall, but if really need alot, can consider BLOB)

After that, when you want to display, at the code behind of the aspx page, retrieve the html as string, and use use

Response.Write()

6 Comments

storing html as string will be better than storing it as binary object, hence my recommendation
I agree, however, since it has special characters, what is the proper way to store it?
If your meaning of special character meant things like ©, when you read in the html file, it should already be parsed as &0169;. Needs testing out, cant confirm. If you are worrying about sql injection, maybe u can pass the value to a stored procedure. Easy maintenance too.
Correct me if I am wrong, but if I do a parameters.addWithValue("@htmlString",theHtmlString), it will strip out some of the characters like (<>*), etc.
@Dima, I think for this U better try it out. I'm more used to Entity Framework, so I tend to stick to entity framework calling stored procedure. But for my method, I didn't have any issue with special characters being stripped out. Simple way to test is that, you can go to the sql browser to insert those characters u mentioned. It will pass. So I believe it should not strip out special characters. I'm also interested to know if it strips them
|
0

You'll be needing the .NET driver for MySQL. If you have experience building C# applications that connect to MSSQL or any other database server, then you'll know where to go next.

As far as storing in the database is concerned, have a text field in your table, to store the HTML code in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.