0

我喜欢用不同的公式填充我的数据库。

当我在数据库中有一个公式时,"(2+4)*5"我得到了结果40

update DB.dbo.INFOGROUP SET formula = '(2+4)*5' where INFOGROUP = 'S42'

当我在数据库中有一个公式时,"(x+4)*5"我得到了结果20x即使我将值设置为 ,该值也会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).

4

1 回答 1

2

脚本引擎不了解您方法中的变量。如果要评估包含变量的公式,则需要告诉它该变量具有什么值:

SC.Language = "VBSCRIPT"
sc.AddCode "Private Const x = 10"

Dim Result As Double = Convert.ToDouble(SC.Eval(Formula))
于 2018-10-07T17:31:18.587 回答