Skip to content

Instantly share code, notes, and snippets.

@NikkiBuck
Created January 27, 2013 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikkiBuck/4651300 to your computer and use it in GitHub Desktop.
Save NikkiBuck/4651300 to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment