Solved

how do you save html text into ms sql table?

Posted on 2014-10-13
7
688 Views
Last Modified: 2014-10-15
Have downloaded long html pages from various website pages and want to save them in ms SQl database table

with update code the the update fails as there all sorts of text markers ' and I'm sure lots of other html items which conflict with the syntax required by uypdate code for ms sql

any ideas on how to save the html page in the sql table field ?
0
Comment
Question by:stephenwilde
  • 4
  • 2
7 Comments
 
LVL 40
ID: 40378531
Supposing that you have downloaded the pages into .html files, you can simply read the file into a String with the StreamReader.ReadToEnd method, and save that in a varchar or nvarchar field.
0
 

Author Comment

by:stephenwilde
ID: 40378707
But when the string goes to update sql code it will still have characters such as ' that will not work properly with the sql update.

currently using this code;

WebClient client = new WebClient();
byte[] webpageraw = null;
webpageraw = client.DownloadData(sURL);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string fullHtml = enc.GetString(webpageraw);

Open in new window


then string fullHtml is saved by SQL "update tablename set html = fullHtml "

but fails as fullhtml has characters such as ' that prevent update.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40381554
Use a parameterized query. It will take care of all the special characters.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:stephenwilde
ID: 40381562
It is the html content of a web page that is being saved into a SQL field via an update statement that is causing the problem not the URL.
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40381585
A parameterized query doesn't care if what you put into the parameter is an URL, HTML, or SQL.
SqlCommand cmd = new SqlCommand("update table set column=@page", connection);
cmd.Parameters.AddWithValue("@page", html);

Open in new window

0
 

Author Comment

by:stephenwilde
ID: 40381591
Thank you for explaining
0
 

Author Closing Comment

by:stephenwilde
ID: 40381592
Thank you
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question