Wednesday, July 14, 2010

Store Scrapped Image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
scraping();
}
}

protected void scraping()
{
//WebClient webClient = new WebClient();
//const string strUrl = "http://www.gotime.com/seattle/find/places/All-Romantic-Places-in-Seattle";
//byte[] reqHTML;
//reqHTML = webClient.DownloadData(strUrl);
//UTF8Encoding objUTF8 = new UTF8Encoding();
//string output = objUTF8.GetString(reqHTML);
//int start = output.IndexOf("
    //int end = output.IndexOf("
", start) + 8 - start;

//output = output.Substring(start, end);
////lblWebpage.Text = output;

string url = "http://places3.assets.gotime.com/7d5ea606b357cf90ae438faf9b21e55aadf2567b_q.jpg";

string filename = url.Substring(url.LastIndexOf('/') + 1);
byte[] bytes = GetBytesFromUrl(url);
WriteBytesToFile(Server.MapPath("~/Image/" + filename), bytes);
}

static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();

Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
}

Wednesday, July 7, 2010

Delete Duplicate Records

SET ROWCOUNT 1
DELETE tblcity
FROM tblcity a
WHERE (SELECT COUNT(*) FROM tblcity b WHERE b.city = a.city and b.countryid='c081') > 1
WHILE @@rowcount > 0
DELETE tblcity
FROM tblcity a
WHERE (SELECT COUNT(*) FROM tblcity b WHERE b.city = a.city and b.countryid='c081' ) > 1
SET ROWCOUNT 0