-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)"
8
  • Example dataset/outcome? What have you already done? Commented Jul 2, 2015 at 18:32
  • Read the code. Put it into a file. Compile the file appropriately. Run the executable. Commented Jul 2, 2015 at 18:34
  • I updated my post with a little more information, but the formulas could be a lot more complex. Commented Jul 2, 2015 at 18:35
  • 1
    @ZoffDino not sure if that is what I need. The goal is to be able to take a string of code and execute it as code. We can disregard the sql part. Commented Jul 2, 2015 at 18:37
  • Do you want to execute it locally (in your C# client), or on the server (as stored procedure)? Commented Jul 2, 2015 at 18:38

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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! ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.