Bulk Inserts em C#
Autor: Theo Alexandre Miliani | Publicado: 2012-05-07T08:44:00-07:00

Existem duas formas de se fazer um bulk insert em C#
A forma com o SQL classico e a forma com o linq.
Nesse exemplo existe tbm a necessidade de se apagar os registros anteriores para se inserir registros novos. 
Eu pessoalmente acredito que a forma de deleção com o linq poderia ser melhorada, ja que para se deletar diversos registros de uma unica vez, fica necessaria um select e um loop com delete de objeto por objeto de registro, enquanto no sql tradicional basta apenas um unico delete ficando o resto do processamento dele para o proprio banco de dados (se alguem souber de alguma forma melhor de se fazer isso sem o loop fique a vontade para dizer).
&nbsp;Como se ve nos codigos:

Com Linq:
int CodUsuario = Convert.ToInt32(Formulario["CodUsuario"]); 

var Deleta = from ObjDeleta in BD.tblAlvo where ObjDeleta.CodUsuario == CodUsuario select ObjDeleta;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (var del in Deleta)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BD.tblAlvo.DeleteObject(del);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BD.SaveChanges();

Com Sql normal
DB.ExecuteStoreCommand("delete from tblAlvo where CodUsuario= " + CodUsuario + "");

Já na inserção nao existe nenhuma grande novidade, exceto que com o linq me pareceu muito mais seguro devido as regras inseridas no entities, enquanto no sql normal a inserção fica vulneravel ao sql injection

Nesse sistema foi inserido um drag'n'drop na tela que o usuario tem acesso para modificar os direitos de acesso para usuarios, que altera um hiddem atraves de jquery, criando uma array Ex. 1,4,7,9,10

com Linq
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int CodUsuario = Convert.ToInt32(Formulario["CodUsuario"]);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Array Menu = Formulario["MenuEspecial"].Split(',');

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (var item in Menu)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var Menus = new tblAlvo();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Menus.CodUsuario = CodUsuario;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Menus.Menu_Cod = Convert.ToInt32(item);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BD.tblAlvo.AddObject(Menus);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BD.SaveChanges();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

com Sql Normal

int CodUsuario = Convert.ToInt32(Formulario["CodUsuario"]);&nbsp; 
Array Menu = Formulario["MenuEspecial"].Split(',');

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (var item in Menu)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DB.ExecuteStoreCommand("insert into tblAlvo (CodUsuario, Menu_Cod (values "+CodUsuario+","+ item +"); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

Embora com menos codificação no modo SQL, o mesmo fica mais exposto a ataques de inserções maliciosas.
Espero ter ajudado
Ate mais...