how to call stored Procedure with Linq

Asked

Viewed 531 times

0

Good morning, everyone. I have a stored procedure in the sql server, and I would like to call you on my application c#, via linq. Since now I appreciate any help

2 answers

2

Create the Stored Procedure in your database.

CREATE PROCEDURE BuscaCliente @nome varchar(255)          
AS
BEGIN
  SELECT * from Cliente where nome = @nome
END
  • In Visual Studio add a file of type LINQ to SQL, in my case I will add with the name Banco.dbml.

  • When creating the file, I added a new connection in Server Explorer pointing to my test database and navigate to the folder Storedprocedures

  • Click and drag the Storedprocedures into the Banco.dbml file

  • Will be added a Method with the SP name, in the code just call the SP

The code below shows the use of Procedure:

BancoDataContext bd = new BancoDataContext();

var resultado = bd.BuscaCliente("celso");
foreach (var r in resultado){
  Console.Write(r.nome);
}
Console.ReadKey();

Source: http://csharpnamarra.blogspot.com.br/2013/04/linq-x-stored-procedure.html

2

Good afternoon guys. I already solved the problem. for those who have the same doubt there goes the solution:

public List<SP_Comb> ListByCliente(int parametro1, string parametro2)
{
  List<SP_Comb> ListaCliente = new List<SP_Comb>();

  var q = DataContext.SP_ComboEntidade(parametro1, parametro2);

  foreach (var lista in q)
  {
    ListaCliente.Add(lista);
  }
  return ListaCliente;
}

where: SP_Comb - mapping of stored Procedure(via sqlmetal) SP_ComboEntidade - stored Procedure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.