Thursday, June 12, 2014

NET : Create Dynamic SqlCommand Parameters

string IDs = "2055,1644,5889";
List<string> IDNumbers = IDs.Split(',').ToList<string>();

StringBuilder sb = new StringBuilder();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;

sb.Append("SELECT * FROM TABLE WHERE (");

//build parameters
foreach (string idNumber in IDNumbers)
{
sb.Append("TABLE.ID = " + ":parm" + idNumber.ToString());
sb.Append(" OR ");
}
sb.Remove(sb.Length - 3, 3);
sb.Append(") ");

foreach (string idNumber in IDNumbers)
{
cmd.Parameters.Add(new OracleParameter("parm" + idNumber.ToString(), idNumber));
}

cmd.ExecuteNonQuery();


No comments:

Post a Comment