-1

Is there a framework out there that can read a string of C# that is pulled from a database and run it as code?

Kind of like a calculator works...

I just want an idea of how hard it is to do something like this. Or to develop something that can make it happen.

Let's say there is a nvarchar column that I return from a database that has something like this in it:

(a + b - c)

In my code I know what a, b, and c are equivalent to, but how do I replace the values and get it to run as code? Essentially it is what a compiler does.

If you want to disregard the SQL part, how would I execute a string with a formula in it as code in my application:

int a = 1;
int b = 2;
int c = 1;
string formula = "(a + b - c)"
Matthew Peterson
  • 325
  • 1
  • 4
  • 18

1 Answers1

1

If you would use SQL server for "calculating", then it would be totally simple. You could put some T-SQL code in your nvarchar database field, and then let it execute it at runtime on the server. (Basically this is then Dynamic SQL)

SQL Police
  • 4,127
  • 1
  • 25
  • 54
  • So you saying if all of the calculations / formulas could be written as sql then i could store them as strings and run them using sp_executesql? How would I handle plugging in the variables of the formulas? – Matthew Peterson Jul 02 '15 at 18:47
  • @matthewpeterson yes exactly, with sp_execute! and even parameters can be passed from outside, thats pretty cool. i'll show you later, i'm out at nightlife currently! ;) – SQL Police Jul 02 '15 at 20:50