我喜欢用不同的公式填充我的数据库。
当我在数据库中有一个公式时,"(2+4)*5"
我得到了结果40
:
update DB.dbo.INFOGROUP SET formula = '(2+4)*5' where INFOGROUP = 'S42'
当我在数据库中有一个公式时,"(x+4)*5"
我得到了结果20
。x
即使我将值设置为 ,该值也会x
被忽略10
:
update DB.dbo.INFOGROUP SET formula = '(x+4)*5' where INFOGROUP = 'S42'
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
Dim SC As New MSScriptControl.ScriptControl
Dim x As Double = 10
SQL.AddParam("@infogroup", "S42")
SQL.ExecQuery($"SELECT formula FROM DB.dbo.INFOGROEP where infogroep=@infogroep")
Dim Formula As String = <string_from_db>
'SET LANGUAGE TO VBSCRIPT
SC.Language = "VBSCRIPT"
'ATTEMPT MATH
Try
Dim Result As Double = Convert.ToDouble(SC.Eval(Formula))
'SHOW THAT IT WAS VALID
MessageBox.Show("Math success, " & Formula & " equals " & Result.ToString)
Catch ex As Exception
'SHOW THAT IT WAS INVALID
MessageBox.Show("Not a valid math formula for a double.")
End Try
End Sub
这可能吗,还是我需要第三方脚本?
顺便说一句:在 Visual Foxpro 中有一个类似EVAL(db_string)
.