I’m writing a general purpose function for feeding query string straight into a sproc. The algorithm is fairly basic – loop through query keys, use them as parameter names while the value are used as parameter values. So it looks like something like this:
ArrayList pars = new ArrayList();
SqlParameter p;
int tryInt;
for (int i = 0; i < req.QueryString.Count; i++)
{
key = req.QueryString.AllKeys[i];
if (int.TryParse(req[key], out tryInt))
{
p = new SqlParameter("@" + key, SqlDbType.Int);
p.Value = tryInt;
pars.Add(p);
}
}
This works fine so far, except that of course all query keys must match the parameters for the sproc, if they don’t I get an SQL exception saying something like
@someParameter is not a parameter for procedure some_sproc
But I need to be able to pass in variables in the query string that won’t be passed into the sproc, so I need a way to “ignore” them.
Is there a way to test whether a given stored procedure expects a certain parameter? So that I can do something along these lines
if (paramExists("@" + key, "some_sproc") && int.TryParse(req[key], out tryInt))
{
p = new SqlParameter("@" + key, SqlDbType.Int);
p.Value = tryInt;
pars.Add(p);
}
you can interrogate the ANSI information_schema.parameters view, it returns all parameters with their position