After part 1 it is time to load the data in the database. We will use linq for it. In a previous article I I have shown how to get and build linq to oracle. With that in mind, we start creating the table. After this we create the linq proxy classes using dbmetal. And finally the code to degrees process.
Let’s start with the table:
drop table deg / create table deg ( degkey number not null, degdat number not null, degtim number not null, degcelcius number not null, constraint deg_pkey primary key (degkey), constraint degdat_fkey foreign key (degdat) references dat (datkey), constraint degtim_fkey foreign key (degtim) references tim (timkey) ) / drop sequence degkey_seq / create sequence degkey_seq start with 1 increment by 1 nomaxvalue / create or replace trigger degkey_trigger before insert on deg for each row begin select degkey_seq.nextval into :new.degkey from dual; end; /
After this we use dbmetal to generate the helper classes:
dbmetal /provider:oracle /conn:”Data Source=XE;User Id=gwe;Password=gwe;” /database=gwe /code:gwe.cs
The problem here is that the databasename is the same as a tablename in the schema. To solve this edit the gwe.cs file and change the DataContext name to GWE (all capitals). Also change the constructors to GWE.
Then the code to the degrees.cs:
using System;
using System.Web.Services.Protocols;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using DbLinq.Linq.Mapping;
using System.Data.OracleClient;
public class Degrees
{
public static void Main (string [] args)
{
try {
string degrees=”";
string success=”";
GlobalWeatherSoapClient w = new GlobalWeatherSoapClient();
// We load the string in an xml reader to parse it and find the temperature
XmlReader reader = XmlReader.Create(new System.IO.StringReader(w.GetWeather(“Groningen”,”Netherlands”)));
reader.MoveToContent();
// Parse the file and find the Temperature element its value
while (reader.Read())
{
if (reader.NodeType==XmlNodeType.Element && reader.Name==”Temperature”)
{
// Do some regex to find the temperature in celcius
reader.Read(); degrees = Regex.Replace(Regex.Replace(reader.Value,”^.*\\(“,”"),” C\\).*$”,”");
}
if (reader.NodeType==XmlNodeType.Element && reader.Name==”Status”)
{
reader.Read(); success = reader.Value.Trim();
}
}
reader.Close();
System.Console.WriteLine(degrees + success);
if (success==”Success”) {
System.DateTime d = System.DateTime.Now;
string connStr = “User Id=gwe;Password=gwe;Data Source=XE”;
GWE db = new GWE(new OracleConnection(connStr));
// get datkey
var q1 = (from p in db.Tims where p.TimHour==d.Hour select p).Single();
var q2 = (from p in db.Dat where p.DatYYYYMmDd==(d.Year*10000)+(d.Month*100)+d.Day select p).Single();
DEG deg = new DEG();
deg.DEGKey=1; // dummy
deg.DEGCeLcIUs=Convert.ToDecimal(degrees);
deg.DEGdAt=q2.DatKey;
deg.DEGTim=q1.TimKey;
db.DEG.InsertOnSubmit(deg);
db.SubmitChanges();
}
}
catch(Exception ex) // catch all exceptions so the batch continues
{
Console.WriteLine(ex.ToString());
}
}
}
build:
csc /r:dblinq.dll,dblinq.oracle.dll degrees.cs gwe.cs GlobalWeather.cs
And schedule every hour in windows.