如何将屏幕显示的网站保存到SQL数据库

问题描述:

因此,在我的Windows窗体中,我创建了一个文本框,用户可使用该文本框输入网站地址,然后单击一个按钮以将该网站剪贴到浏览器控件中.现在,我要做的就是将屏幕抓取的网站保存到sql数据库中.我将如何去做呢?

这是我的代码wbHtmlpage是我的Web浏览器的名称.

btnRead是我从数据库中读取文件的地方

So in my windows form I''ve made a textbox which the user uses to enter the website address and then click a button to screen scrap that website into a browser control. Now what I want to do is save that screen scraped website to an sql database. How would I go about doing this?

Heres my code wbHtmlpage is the name of my web browser.

EDIT 2: btnRead is where i''m reading the file from the database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Data.SqlClient;

namespace FilmAndEntertainmentSystem
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private string GetWebsiteHtml(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string result = reader.ReadToEnd();
            stream.Dispose();
            reader.Dispose();
            return result;
        }

        private void btnGetHTML_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
        }

        private void btnScreenSave_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);

            // set up data connection

            SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");

            // Set up adapter manager

            SqlDataAdapter da = new SqlDataAdapter();

            using (SqlCommand com = new SqlCommand("INSERT INTO Website (WebsiteImage) VALUES (@Image)", cs))
            {
                com.Parameters.AddWithValue("@Image", bytes);
                cs.Open();

                com.ExecuteNonQuery();

                cs.Close();
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);

            // set up data connection

            SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");

            // Set up adapter manager

            SqlDataAdapter da = new SqlDataAdapter();

            // Data set
            DataSet ds = new DataSet();

            da.SelectCommand = new SqlCommand("Select WebsiteImage From Website Where WebsiteID = 3", cs);

            da.Fill(ds, "Website");

            response.ContentType = "Image";
            response.BinaryWrite(bytes);

            cs.Open();
            cs.Close();



        }
    }
}

根据数据的大小,在表中使用图像数据类型,然后从WebBrowser控件中读取数据.页面的HTML作为字符串.
然后将其转换为字节:
Depending on the size of the data, use an image datatype in your tables, and read the data from the WebBrowser control.DocumentText - this gives you the HTML for the page as a string.
Then just convert it to bytes:
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);

并将其插入数据库.

and insert it to your database.

using(SqlCommand com = new SqlCommand("INSERT INTO myTable (scrapedData) VALUES (@SD)", con))
   {
   com.Parameters.AddWithValue("@SD", bytes);
   s.ExecuteNonQuery();
   }



[edit]将字符串"s"传递给参数,而不是字节[]"bytes"-OriginalGriff [/edit]


是的,我知道,但是我如何将其读取到Web浏览器控件上?"




[edit]Passed string "s" to parameters instead of byte[] "bytes" - OriginalGriff[/edit]


"Yeah i know but how do I read it onto the web browser control?"


Response.ContentType = "image/JPEG";
Response.BinaryWrite(myBytesFromDataBase);




它仍然无法正常工作,无法识别响应或BinaryWrite.我已将代码放在问题中,以便您查看."

也许,如果在从数据库读取数据并将其写入响应之间的某个位置,您实际上是在使用数据库中的信息,则它可能比HTML更好地用作图像...




"It still isnt working, its not recognizing the response or the BinaryWrite. I''ve put my code in the question so you can take a look."

Perhaps, if somewhere between reading the data from the database and writing it into the response, you actually used the info from the database, it might work better as an image than the HTML does...

        private void btnRead_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);
 
...

            da.SelectCommand = new SqlCommand("Select WebsiteImage From Website Where WebsiteID = 3", cs);
            da.Fill(ds, "Website");

--->>> Perhaps a bit of code to use your database info here, might help a bit!
 
            response.ContentType = "Image";
            response.BinaryWrite(bytes);
 
            cs.Open();
            cs.Close();
        }

我还建议您在尝试从中读取连接"cs"之前,而不是在...之后更有用地打开它::laugh:

I would also suggest that your connection "cs" might be more usefully opened before you try to read from it, rather than after... :laugh:


您可能想使用Blob字段-某些网页可能很大.
You probably want to use a blob field - some web pages can be huge.