SqlCommand sc = new SqlCommand(); sc.Connection = this.oConnection; sc.Connection.Open(); foreach (DataRow Row in ds.Tables[0].Rows) { switch (Row.RowState) { case DataRowState.Added : // or use a StoredProc, which I prefer sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )"; sc.Parameters.Clear(); sc.Parameters.Add("@xyz", Row["xyz"]); sc.Parameters.Add("@abc", Row["abc"]); sc.ExecuteNonQuery(); break; // Do the same for DataRowState Deleted and Modified case DataRowState.Deleted : break; case DataRowState.Modified : break; } } sc.Connection.Close();But this line...
sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
could be up here instead....SqlCommand sc = new SqlCommand(); sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )"; <<<--------------- sc.Connection = this.oConnection; sc.Connection.Open(); foreach (DataRow Row in ds.Tables[0].Rows) { . . .Then the code in the foreach only needs the param info, right?
>>sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )"; >>sc.Parameters.Clear(); >>sc.Parameters.Add("@xyz", Row["xyz"]); >>sc.Parameters.Add("@abc", Row["abc"]); >>sc.ExecuteNonQuery(); >>
>>sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
>>sc.ExecuteNonQuery();
>
>>"Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
>
>>sc.Parameters.Clear(); >sc.Parameters.Add("@xyz", Row["xyz"]); >sc.Parameters.Add("@abc", Row["abc"]); >>