如何在SQL Server中存储数学公式 [英] how to Store Math Formula in sql server

查看:462
本文介绍了如何在SQL Server中存储数学公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储公式文本并打算评估公式并显示操作结果..

请帮助我!

非常感谢

I Wanted to store the formula text and intend to evaluate the formulae and display results of the operation..
Please help me!
Thanks a lot

推荐答案

你可以这样做...



在sql表中存储公式 tblFormulas 如下所示...

you can do this way...

store formula in sql table tblFormulas like below...
FormulaName  			FormulaEquation
Gain				S.P. - C.P.
Loss				C.P. - S.P.
Gain%				(GAIN * 100)/C.P.
Loss%			         (LOSS * 100)/S.P
Selling Price (S.P)                 ((100 + GAIN%)/100)*C.P.



现在...

在.net中写下代码

例如


Now...
Write down code in .net
replace values of parameters in equations...
for e.g.

string Equation = "((100 + GAIN%)/100)*C.P."; 
Equation = Equation.Replace("GAIN%", txtGainPer.text); // suppose txtGainPer.text = 50
Equation = Equation.Replace("C.P.", txtCP.text); // suppose txtGainPer.text = 27
Equation = Equation.Replace("S.P.", txtSP.text);
Equation = Equation.Replace("GAIN", txtGain.text);
Equation = Equation.Replace("LOSS", txtLoss.text);
//result will be like below... Equation = "((100 + 50)/100)*27";
//now send this equation to sql as below to evaluate
Equation = "Select " + Equation;
//then pass string to sql for execution...



注意:此代码容易受到 sql注入

提示:为了避免这种情况,您可以使用 第三方.net类 可用于评估表达式或者 为您自己的评估表达式编写代码



希望这对你有所帮助。

快乐编码!

:)


NOTE : This code is vulnerable for sql injection
TIP: to avoid this you can use third party .net classes available for evaluate expressions OR can write code for evaluate expressions your self

Hope this helped you.
Happy Coding!
:)


如果我没错;你想在一个字段中存储一些sql并执行它。



检查这可能会对你有帮助。



If I am not wrong; you want to store some sql in a field and execute it.

Check this it may help you.

Declare @a varchar(999)

set @a='select 5*5+5'
--You can use select @a=fieldname from tablename where criterya

Exec (@a)


这篇关于如何在SQL Server中存储数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆