我正在使用Visual Studio中的TableAdapter查詢配置嚮導編寫以下SQL查詢。使用ISNULL的SQL查詢
SELECT COUNT(*) AS census
FROM Inventory INNER JOIN Taxonomy ON Inventory.GlobalID = Taxonomy.GlobalID
WHERE (Inventory.Institution = @institution) AND (Inventory.Year = @year) AND
(Inventory.Nending > 0)
我試圖以下標準添加到WHERE子句:
(Taxonomy.Class = ISNULL(@class, Taxonomy.Class))
,使得任一
1)僅行匹配@class輸入參數被退回或
2 )無論其TaxonomyGlobal.Class值如何,都將返回所有行。
當我將此語句添加到查詢中時,調用查詢的C#代碼拋出System.ArgumentNullException錯誤,並聲明@class值不能爲空。
任何有關如何將此標準添加到WHERE子句的幫助將不勝感激。
C#代碼:
namespace CollectionMetrics
{
class DatabaseQueries
{
QueryDataSetTableAdapters.InventoryTableAdapter queryAdapter =
new QueryDataSetTableAdapters.InventoryTableAdapter();
public void CensusQuery(string institution, short year, string xclass)
{
int census = 0;
string localClass = xclass;
if (xclass == "All Classes") localClass = null;
census = (int)queryAdapter.CensusBySpecies(localClass, institution, year);
censusOutput.Add(census);
}
}
}
ISNULL不會告訴你expressin是否爲null,而只是用指定值替換空值。 –
@DavidP,我認爲他明白了。再次仔細查看ISNULL在這裏的使用情況。 –
請提供與此問題相關的完整C#代碼,這將有助於我們更好地理解您 – Surendra