C# DbCommand in a loop
c#, enterprise-library
Solution
DbCommand insert = db.GetStoredProcCommand("Insert");
foreach (string ID in myIDs)
{
insert.Parameters.Clear();
db.AddInParameter(insert, "FileName", System.Data.DbType.String,
ID + ".xml");
db.ExecuteNonQuery(insert, transaction);
}
You could also just add the Parameter once outside the loop, and then change its Value inside the loop. Half of one, six dozen of the other.
Problem
I have a collection of files, for each file I am going to call an SP using Dbcommand in a transaction. for example: ``` DbCommand insert = db.GetStoredProcCommand("Insert"); db.AddInParameter(insert, "FileName", System.Data.DbType.String, ID + ".xml"); db.ExecuteNonQuery(insert, transaction); ``` My question is how do I put this in a loop? The answer below doesn't work, but thanks for the otherwise great code sample. Problem is db doesn't have a Parameters collection that you can manipulate. check... http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.data.sql.sqldatabase_members%28BTS.10%29.aspx I am declaring my database like this: ``` SqlDatabase db = new SqlDatabase(this.ConnectionString ); ```