views:

713

answers:

2

Normally, when you want to call a stored procedure directly through Linq to Sql, you can use the ExecuteQuery method:

result = dc.ExecuteQuery<MyTable>("Exec myStoredProcedure");

And if you need to call it with parameters, you can add them through string substitution:

string query = "Exec myStoredProcedure ";
for (int i = 0; i < parameters.Count - 1; i++) {
  query += " {" + i + "},";
}
query = query.TrimEnd(',');
result = dc.ExecuteQuery<MyTable>(query, parameters);

But what if one of the parameters is an output variable? Is it possible to get the value back after the procedure has been run?

A: 

I'm not sure, but you can try to declare variable in query, pass it as an output parameter and then select it:

//assuming you out parameter is integer
string query = "DECLARE @OUT INT ";
query += " Exec myStoredProcedure ";
for (int i = 0; i < parameters.Count - 1; i++) {
  query += " {" + i + "},";
}
//assuming the output parameter is the last in the list
query += " @OUT OUT ";
//select value from out param after sp execution
query += " SELECT @OUT"
Max Gontar
A: 

You don't need to write raw SQL for StoredProcedures in ExecuteQuery. You can map StoredProcedures in *.dbml and you can use StoredProcedures as Methods.

Alper Ozcetin
dbml-based stored procedures don't support output parameters, as far as I can tell.
Adam Lassek