Hi All,
I am a complete novice at programming. I am supposed to setup a sql database which is updated on a daily basis. I have managed to write the back end code which is in C#. My table looks like following
ID Date Value Cum
168 1/1/2011 0.2 0.2
290 1/1/2011 0.1 0.3
300 1/1/2011 0.5 0.5
168 1/2/2011 0.1
290 1/2/2011 0.5
450 1/2/2011 0.9
I get the values of ID,date and value from the web and these are inserted into the database.I am maintaining the database from the 1st of jan.So the cum of all values of Jan1 are the same as their values but from the 2nd, wherever the IDs match the cumulative should be the added values.(Eg for 168,1/2/2011 Cum should 0.2+0.1 =0.3) and if they don't match the number in the value column should also be inserted into the Cum column.(For 450,1/2/2011,Cum = 0.9).
Can a anyone suggest the process and sql query to do this? I am attaching a snippet of the code.
string YYYY = startDate.Year.ToString();
string MM = startDate.Month.ToString();
string DD = startDate.Day.ToString();
if (MM.Length == 1) { MM = "0" + MM; }
if (DD.Length == 1) { DD = "0" + DD; }
File.Copy(@"C:\WebFireModel\weatherData\nws_precip_1day_observed_" + YYYY + "" + MM + "" + DD + ".dbf", @"C:\WebFireModel\weatherData\" + YYYY + "" + MM + "" + DD + ".dbf");
String ConnectionString;
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\WebFireModel\\weatherData;Extended Properties=dBase IV";
System.Data.OleDb.OleDbConnection dBaseConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);
dBaseConnection.Open();
System.Data.OleDb.OleDbCommand dBaseCommand = new System.Data.OleDb.OleDbCommand(" SELECT * FROM C:\\WebFireModel\\weatherData\\" + YYYY + "" + MM + "" + DD + ".dbf as US,C:\\WebFireModel\\weatherData\\se_hrap.dbf as se where US.Id = se.Id ", dBaseConnection);
DataGrid DataGrid1 = new DataGrid();
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = dBaseCommand;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
dBaseConnection.Close();
string your_connectionstring = ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString();
foreach (DataGridItem dgi in DataGrid1.Items)
{
int Id = Convert.ToInt32(dgi.Cells[0].Text.Trim());
int Hrapx = Convert.ToInt32(dgi.Cells[1].Text.Trim());
int Hrapy = Convert.ToInt32(dgi.Cells[2].Text.Trim());
double Lat = Convert.ToDouble(dgi.Cells[3].Text.Trim());
double Lon = Convert.ToDouble(dgi.Cells[4].Text.Trim());
double pre = Convert.ToDouble(dgi.Cells[5].Text.Trim());
SqlConnection conn = new SqlConnection(your_connectionstring);
conn.Open();
SqlCommand comm1 = new SqlCommand("insert into dbo.Precipitation(Id,Value,date) values (" + Id + "," + pre + ", " + "'" + MM + "/" + DD + "/" + YYYY + "'"+")", conn);
comm1.ExecuteNonQuery();