如何从SQL数据库中将屏幕抓取的数据读取到Web浏览器控件上? [英] How do I read screen scraped data from an sql database onto a web browser control?

查看:143
本文介绍了如何从SQL数据库中将屏幕抓取的数据读取到Web浏览器控件上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的Windows窗体中,我创建了一个文本框,用户可使用该文本框输入网站地址,然后单击一个按钮以将该网站剪贴到浏览器控件中.他们还可以将该屏幕抓取的网站保存到sql数据库.我现在想做的是将数据从sql数据库读取到Web浏览器控件上.现在,屏幕抓取页面以图像形式保存到数据库,并以二进制数据的形式显示在数据库中.我有一些用于此的代码,但是我不确定如何将其读取到浏览器控件中.

这是我的代码

到目前为止,我为阅读所做的代码显示在名为btnRead的方法中.
wbHtmlPage是我的Web浏览器控件的名称.

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. They can also save that screen scraped website to an sql database. What I want to do now is read that data from the sql database onto the web browser control. Now the screen scraped page is saved to database as an image and is display in the database as binary data. I have some of the code for this, but i''m not sure how to read it to the browser control.

Here is my code

The code that I have done so far for the reading is displayed in the method called btnRead.
wbHtmlPage is the name of my web broswer control.

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");
 
            cs.Open();
 
            response.ContentType = "Image";
            response.BinaryWrite(bytes);
 
            
            cs.Close();
 
 
 
        }
    }
}

推荐答案

如果将其另存为图像,则可以执行以下操作以使其在屏幕上显示(当然使用适当的ImageFormat枚举):

If it''s saved as an image, you could do this to get it on the screen (using the appropriate ImageFormat enum of course):

// Set the content type and return the image
Response.ContentType = "image/GIF";
bytes.Save(Response.OutputStream, ImageFormat.Gif);


这篇关于如何从SQL数据库中将屏幕抓取的数据读取到Web浏览器控件上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆