Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void RefreshPlot()
- {
- string query = "SELECT Count(*) AS count, plot_id FROM booking WHERE postcode=@postcode AND " +
- "status='open' GROUP BY plot_id";
- var cmd = new MySqlCommand(query, DbConnect.Connection);
- cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
- var da = new MySqlDataAdapter(cmd);
- var dtCounts = new DataTable();
- da.Fill(dtCounts);
- if (dtCounts.Rows.Count > 0)
- {
- query = "UPDATE plot SET jobs = @jobCount WHERE plot_id = @plotID AND postcode=@postcode;";
- query += "UPDATE plot " +
- "LEFT JOIN booking " +
- "ON plot.plot_id = booking.plot_id " +
- "SET plot.jobs = 0 " +
- "WHERE plot.postcode=@postcode " +
- "AND booking.plot_id IS NULL;";
- query += "update plot p " +
- "inner join " +
- "(select sum(case when status = 'Open' then 1 else 0 end) cnt, plot_id " +
- "from booking group by plot_id) p2 on p.plot_id = p2.plot_id " +
- "set p.jobs = p2.cnt;";
- cmd = new MySqlCommand(query, DbConnect.Connection);
- foreach (DataRow row in dtCounts.Rows)
- {
- cmd.Parameters.Clear();
- cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
- cmd.Parameters.AddWithValue("@jobCount", int.Parse(row["count"].ToString()));
- cmd.Parameters.AddWithValue("@plotID", int.Parse(row["plot_id"].ToString()));
- cmd.ExecuteNonQuery();
- }
- }
- else if ((dtCounts.Rows.Count == 0))
- {
- query = "UPDATE plot SET jobs=0 WHERE postcode=@postcode;";
- cmd = new MySqlCommand(query, DbConnect.Connection);
- cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
- cmd.ExecuteNonQuery();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement