Saturday, November 29, 2008

WebPage / Screen Scraping Html to Sql-Server, code sample VB.NET asp.net 2.0

This is the sample in vb asp.net 2.0, sql-server express 2005 if you want to scrap page from url want to insert in sql-server table.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="htmltosql.aspx.vb" Inherits="htmltosql" validateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" id="amzTop" TextMode="MultiLine" Columns="360" Rows="15" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />

</div>
</form>
</body>
</html>


Code Behind

Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Xml
Imports System.Net


Partial Class htmltosql
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ,Button1.Click
insertdata()

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim req As HttpWebRequest = WebRequest.Create("http://www.yahoo.com/")
Try
'Get the data as an HttpWebResponse object
Dim resp As HttpWebResponse = req.GetResponse()
'Convert the data into a string (assumes that you are requesting text)
Dim sr As New StreamReader(resp.GetResponseStream())
Dim results As String = sr.ReadToEnd()
sr.Close()
amzTop.Text = results
Catch wex As WebException
Response.Write("<font color=red>SOMETHING WENT AWRY!<br />Status: " & wex.Status & "Message: " & wex.Message & "</font>")
End Try
End Sub

Public Sub insertdata()

'Response.Write(amzTop.Text)
'Response.End()


Dim con As New SqlConnection("Data Source=testserver\SQLEXPRESS;Initial Catalog=test_DV;Integrated Security=True")
Dim cmd As New SqlCommand("insbulkhtml", con)

cmd.CommandType = Data.CommandType.StoredProcedure

cmd.Parameters.AddWithValue("@htmlstr", LTrim(amzTop.Text))
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub


End Class

stored proc

Create proc [dbo].[insbulkhtml]
@htmlstr nvarchar(max) as
delete from tem_tab1
INSERT into tem_tab1
select @htmlstr


Table

CREATE TABLE [dbo].[tem_tab1](
[Prodcode] [nvarchar](max) NULL
) ON [PRIMARY]

No comments: