Created
January 27, 2013 23:28
-
-
Save NikkiBuck/4651300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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