Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1.     public static void RefreshPlot()
  2.             {        
  3.                 string query = "SELECT Count(*) AS count, plot_id FROM booking WHERE postcode=@postcode AND " +
  4.                                "status='open' GROUP BY plot_id";
  5.                 var cmd = new MySqlCommand(query, DbConnect.Connection);
  6.                 cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
  7.    
  8.                 var da = new MySqlDataAdapter(cmd);
  9.                 var dtCounts = new DataTable();
  10.                 da.Fill(dtCounts);
  11.    
  12.                 if (dtCounts.Rows.Count > 0)
  13.                 {
  14.                     query = "UPDATE plot SET jobs = @jobCount WHERE plot_id = @plotID AND postcode=@postcode;";
  15.    
  16.                     query += "UPDATE plot " +
  17.                              "LEFT JOIN booking " +
  18.                              "ON plot.plot_id = booking.plot_id " +
  19.                              "SET plot.jobs = 0 " +
  20.                              "WHERE plot.postcode=@postcode " +
  21.                              "AND booking.plot_id IS NULL;";
  22.    
  23.                     query += "update plot p " +
  24.                              "inner join " +
  25.                              "(select sum(case when status = 'Open' then 1 else 0 end) cnt, plot_id " +
  26.                              "from booking group by plot_id) p2 on p.plot_id = p2.plot_id " +
  27.                              "set p.jobs = p2.cnt;";
  28.    
  29.                     cmd = new MySqlCommand(query, DbConnect.Connection);
  30.                     foreach (DataRow row in dtCounts.Rows)
  31.                     {
  32.                         cmd.Parameters.Clear();
  33.                         cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
  34.                         cmd.Parameters.AddWithValue("@jobCount", int.Parse(row["count"].ToString()));
  35.                         cmd.Parameters.AddWithValue("@plotID", int.Parse(row["plot_id"].ToString()));
  36.                         cmd.ExecuteNonQuery();              
  37.                     }
  38.                 }
  39.                 else if ((dtCounts.Rows.Count == 0))
  40.                 {
  41.                     query = "UPDATE plot SET jobs=0 WHERE postcode=@postcode;";
  42.                     cmd = new MySqlCommand(query, DbConnect.Connection);
  43.                     cmd.Parameters.AddWithValue(("@postcode"), DbConnect.Plot);
  44.                     cmd.ExecuteNonQuery();
  45.                 }
  46.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement