
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.04 KB | hits: 14 | expires: Never
Is there a way to check if a parameter is in stored procedure?
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);
}
}
@someParameter is not a parameter for procedure some_sproc
if (paramExists("@" + key, "some_sproc") && int.TryParse(req[key], out tryInt))
{
p = new SqlParameter("@" + key, SqlDbType.Int);
p.Value = tryInt;
pars.Add(p);
}
SELECT parameter_name, ordinal_position FROM INFORMATION_SCHEMA.parameters
WHERE SPECIFIC_NAME = 'some_sproc'
using (SqlConnection sqlConn = new SqlConnection(yourConnStr))
using (SqlCommand sqlCmd = new SqlCommand(yourProcedureName, sqlConn))
{
sqlConn.Open();
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(sqlCmd);
// now you can check parameters in sqlCmd.Parameters
}