// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.IO; using System.Net; using System.Data; using System.Data.SqlClient; using ICSharpCode.SharpZipLib.Zip; namespace E_Geo { public class GeoLoad { /* START CONFIGURATION */ private const String UrlWebHostingInfo = "http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip"; private const String UrlMaxMind = "http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"; private const String WorkDir = @"C:\e"; private const String ConnStr = "server=ARNEPC2;Integrated Security=SSPI;database=Test"; /* END CONFIGURATION */ private static void Download(string urlstr, string zipfnm) { WebClient wc = new WebClient(); wc.DownloadFile(urlstr, WorkDir + @"\" + zipfnm); } private static void Unzip(string zipfnm, string csvfnm) { Stream s = new FileStream(WorkDir + @"\" + csvfnm, FileMode.Create, FileAccess.Write); ZipInputStream zis = new ZipInputStream(new FileStream(WorkDir + @"\" + zipfnm, FileMode.Open, FileAccess.Read)); ZipEntry ze = zis.GetNextEntry(); byte[] b = new byte[100000]; int n; while((n = zis.Read(b, 0, b.Length)) > 0) { s.Write(b, 0, n); } zis.Close(); s.Close(); } private static void DbImport(string csvfnm, int fromcol, int tocol, int countrycol, string tblnam) { SqlConnection con = new SqlConnection(ConnStr); con.Open(); SqlCommand drp = new SqlCommand("DROP TABLE " + tblnam, con); try { drp.ExecuteNonQuery(); } catch(Exception) { /* we will end here first time */ } SqlCommand cre1 = new SqlCommand("CREATE TABLE " + tblnam + "(id INTEGER NOT NULL, ip1 NUMERIC(10,0), ip2 NUMERIC(10,0), country CHAR(2), PRIMARY KEY(id))", con); cre1.ExecuteNonQuery(); SqlCommand cre2 = new SqlCommand("CREATE INDEX ixip1 ON " + tblnam + "(ip1)", con); cre2.ExecuteNonQuery(); SqlCommand cre3 = new SqlCommand("CREATE INDEX ixip2 ON " + tblnam + "(ip2)", con); cre3.ExecuteNonQuery(); SqlCommand ins = new SqlCommand("INSERT INTO " + tblnam + " VALUES(@id,@ip1,@ip2,@country)", con); ins.Parameters.Add("@id", SqlDbType.Int); ins.Parameters.Add("@ip1", SqlDbType.Decimal); ins.Parameters.Add("@ip2", SqlDbType.Decimal); ins.Parameters.Add("@country", SqlDbType.VarChar); StreamReader sr = new StreamReader(WorkDir + @"\" + csvfnm); int n = 0; string line; while((line = sr.ReadLine()) != null) { n++; string[] cols = line.Split(",".ToCharArray()); decimal fromval = decimal.Parse(cols[fromcol-1].Replace('"', ' ').Trim()); decimal toval = decimal.Parse(cols[tocol-1].Replace('"', ' ').Trim()); string code = cols[countrycol-1].Replace('"', ' ').Trim(); ins.Parameters["@id"].Value = n; ins.Parameters["@ip1"].Value = fromval; ins.Parameters["@ip2"].Value = toval; ins.Parameters["@country"].Value = code; ins.ExecuteNonQuery(); } sr.Close(); con.Close(); } private static void LoadWebHostingInfo() { Download(UrlWebHostingInfo, "whi.zip"); Unzip("whi.zip", "whi.csv"); DbImport("whi.csv", 1, 2, 3, "whi"); } private static void LoadMaxMind() { Download(UrlMaxMind, "mm.zip"); Unzip("mm.zip", "mm.csv"); DbImport("mm.csv", 3, 4, 5, "mm"); } public static void Main(string[] args) { LoadWebHostingInfo(); LoadMaxMind(); } } }