Skip to content

Instantly share code, notes, and snippets.

@NikkiBuck
Created January 27, 2013 23:28

Revisions

  1. NikkiBuck created this gist Jan 27, 2013.
    49 changes: 49 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    public static int CountBirds(string baseBirds = "*", string baseBirder = "*", string baseRegion = "*", string baseDate = "*")
    {
    SqlCommand selectCommand;

    string sql = "SELECT SUM(BirdCount.Counted) FROM Bird INNER JOIN BirdCount ON Bird.BirdID = BirdCount.BirdID " +
    "INNER JOIN Birder ON BirdCount.BirderID = Birder.BirderID INNER JOIN Region ON BirdCount.RegionID = Region.RegionID " +
    "where BirdCount.BirdID ='" + baseBirds + "' AND BirdCount.BirderID = '" + baseBirder + "' AND BirdCount.RegionID = '" + baseRegion + "' AND BirdCount.CountDate = '" + baseDate + "'";
    if (baseBirds == "*")
    {
    sql.Replace("BirdCount.BirdID ='" + baseBirds + "' AND", String.Empty);
    }

    if (baseBirder == "*")
    {
    sql.Replace("BirdCount.BirderID = '" + baseBirder + "' AND", String.Empty);
    }

    if (baseRegion == "*")
    {
    sql.Replace("BirdCount.RegionID = '" + baseRegion + "' AND", String.Empty);
    }

    if (baseDate == "*")
    {
    sql.Replace("BirdCount.CountDate = '" + baseDate + "'", String.Empty);
    }

    if (baseBirds == "*" && baseBirder == "*" && baseRegion == "*" && baseDate == "*")
    {
    sql = "SELECT SUM(BirdCount.Counted) FROM Bird INNER JOIN BirdCount ON Bird.BirdID = BirdCount.BirdID " +
    "INNER JOIN Birder ON BirdCount.BirderID = Birder.BirderID INNER JOIN Region ON BirdCount.RegionID = Region.RegionID ";
    }

    selectCommand = new SqlCommand(sql, new SqlConnection(connectingString));
    try
    {
    selectCommand.Connection.Open();
    return Convert.ToInt32(selectCommand.ExecuteScalar());
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    selectCommand.Connection.Close();

    }
    }