How to save a formula in sqlserver and use this formula in an application

How to save a formula in a table in my operation, I used for the formula (Formula I can be dynamically created by the user) formula

Variabl1+Variable2*3.2+200 

this formula is not const.

thanks

+4
source share
2 answers

I think you need to save exactly the formula in the database, and when you want to use it, process the formula line using the replace method. you should get Variabl1, Variable2 data and use this code:

 string f="Variabl1+Variable2*3.2+200"; f= System.Text.RegularExpressions.Regex.Replace(f, "Variabl1", TxBxVar1.Text); f= System.Text.RegularExpressions.Regex.Replace(f, "Variabl2", TxBxVar2.Text); 

and then process f to run the formula. to run you can use

 System.Text.RegularExpressions.Regex.Split(f, "+"); 

and use this replacement code for all the math operators you want.

Note: this is my way. but maybe there are other ways in C # that I don't know.

+1
source

In C #, you can use the codeDOM services to dynamically compile C # code, or you can dynamically build an expression tree by parsing a formula line yourself. But it is possible that in both cases there is no performance, because compilation at runtime is necessary ( huh, except, maybe this one can work quite well ). Therefore, because of this, I would prefer to embed some scripting engine in the .NET framework - for example, Lua or Javascript . However, if you are writing an asp.net application - why can't you omit the direct javascript code that computes the result of the formula on the client side? This would probably be the best solution, because client side web applications and scripts look like husband and wife :-)

+1
source

Source: https://habr.com/ru/post/1394752/


All Articles