简体   繁体   English

C#:如何创建和使用方程式数据库?

[英]C#: How to create and use a database of equations?

I've been trying to create a database of equations that my windows form application will supply variables to and then calculate the solution. 我一直在尝试创建一个方程式数据库,我的windows表单应用程序将提供变量,然后计算解决方案。 I was trying to store my equations in a text file and load them into a list but I haven't been able to think of how to translate my equation from a string to an actual equation. 我试图将我的方程式存储在一个文本文件中并将它们加载到一个列表中,但我还没有想到如何将我的方程从一个字符串转换为一个实际的方程式。

So, does anyone know how I could store and call on a collection of equations that use variables from my windows form application. 那么,有没有人知道如何存储和调用一组使用我的Windows窗体应用程序中的变量的方程式。

PS Please keep it simple or explain thing fully. PS请保持简单或完整解释事情。 I get confused easily. 我很容易混淆。

Your best bet is to use a math library to evaluate your math expressions. 您最好的选择是使用数学库来评估您的数学表达式。

Here are some links to get you started: https://stackoverflow.com/questions/1387430/recommended-math-library-for-c-net 以下是一些可以帮助您入门的链接: https//stackoverflow.com/questions/1387430/recommended-math-library-for-c-net

You can also take a look in http://ncalc.codeplex.com/ that may be exaclty what you want. 您还可以查看http://ncalc.codeplex.com/ ,这可能是您想要的。

You can do this using dynamic SQL. 您可以使用动态SQL执行此操作。 Here is how in SQL Server. 这是SQL Server中的方法。

You can store the equation in a table, along with the variables that it needs (or if you have a fixed set of variables, that's fine). 您可以将等式存储在表中,以及它需要的变量(或者如果您有一组固定的变量,那就没问题)。

Fetch the equation into a C# string. 将等式获取为C#字符串。

Then construct the following statement: 然后构造以下语句:

select <equation>
from (select <val1> as variable1, <val2> as variable2) t

Where you put in the equation where is and the values of the variables where the values are. 您在其中放入等式的位置和值所在的变量的值。

Execute this statement in the database. 在数据库中执行此语句。 Voila! 瞧! The result will be the result of the equation. 结果将是等式的结果。

There are some downsides to this approach. 这种方法有一些缺点。 It requires two round trips to the database (one to get the equation and the other to run the equation). 它需要两次往返数据库(一个用于获得等式,另一个用于运行等式)。 You are also limited to the functionality of the database. 您还受限于数据库的功能。 Also, if you are not using SQL Server, the syntax could be different (for instance, in Oracle, the inner subquery would have "from dual" in it). 此外,如果您不使用SQL Server,则语法可能不同(例如,在Oracle中,内部子查询将具有“from dual”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM