I have a method that I want to use differently based on the type of input. This is what I have:
public static DataTable GetS(string source = null, string customer = null)
{
if (source != null && customer == null)
{
return GetDataTableFromQuery("db.GetS", new object[] { "@source", source });
}
else if (source == null && customer != null)
{
return GetDataTableFromQuery("db.GetS", new object[] { "@customer", customer });
}
else
{
throw new Exception("Bad input. Call GetS with GetS(source) or GetS(null,customer).");
}
}
The sp looks like this:
CREATE PROCEDURE [db].[GetS]
@source as nvarchar(128) = NULL,
@customer as nvarchar(128) = NULL
AS
BEGIN
IF @customer IS NULL
BEGIN
SELECT
*
FROM
db.S
WHERE
[Source] = @source
END
IF @source IS NULL
BEGIN
SELECT
*
FROM
db.S
WHERE
customer = @customer
END
END
This works fine for GetS(source) and GetS(null,customer) but I have 2 issues.
- it goes bad if someone were to call with
GetS(customer) - it doesn't look very pretty..
is there some way of doing something like this(pseudo-code):
public static DataTable GetS(string input)
{
if(input is sql-uniqueidentifier)
{
return GetDataTableFromQuery("db.GetS", new object[] { "@source", input});
}
else
{
return GetDataTableFromQuery("db.GetS", new object[] { "@customer", input]);
}
}
or is there a better way? (sure, I could make 2 separate methods, but I'd like to make it work with just one Get-method. Feels weird if I make a GetSBySource or something).
sql-identifier?sourceis suppose to be a unque identifier then why is it astringand not aGuid?Guid.TryParsefor the return value.