Is Database.Sql() the recommended method for executing SQL Server Stored procedures

Is Database.Sql() the recommended method for executing SQL Server Stored procedures

rdmrdm Posts: 194Questions: 55Answers: 4

I am trying to execute a SQL Server stored procedure after a particular field is updated via inline editing.

Looking through the .NET Libraries API Documentation, I found a potential method Database.Sql().

While the code shown below works in my case (C# MVC 5 and SQL Server) My question is whether use of Database.Sql() is the optimal method for executing a stored procedure. I just wanted to make sure there wasn't a better method available that I missed in the documentation.

The overall code is inspired by the Events (.NET) page.

      response.PostEdit += (sender, e) => ForwardPopulate(db,e.Id,e.Values);

Where ForwardPopulate is a custom function that executes the SQL Server stored procedure.

private static void ForwardPopulate(Database db, object id, Dictionary<string, object> values)
{
    // ... code ...

    // ... sp is the T-SQL method string for executing a stored procedure  ...
    var sp = $"exec dbo.TestTable {param1},{param2},'{param3}',{param4},'{param5}';";

        // execute stored procedure
    db.Sql(sp);
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,938Questions: 1Answers: 10,157 Site admin
    Answer ✓

    You are correct - if you want to use the Editor Database class, then its Sql() method is the way to execute raw SQL statements.

    Allan

This discussion has been closed.