Thursday, February 8, 2007

Connecting commands to sql connection during runtime

Suppose you have a form containing a number of data adapters, each linked to few sql command classes.
You have a single sql connection class and you want to link all commands to this connection on runtime:

foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic & BindingFlags.Instance))
{
if (fi.FieldType == typeof(SqlCommand))
(fi.GetValue(this) as SqlCommand).Connection = sqlConnection1;
}

No comments: