Solved

how do you save html text into ms sql table?

Posted on 2014-10-13
7
642 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now