C# 中的动态数据结构

发布于 2024-10-11 22:55:54 字数 776 浏览 2 评论 0原文

我的数据库中有数据,我的代码使用 LINQ to Entities 访问它。

我正在编写一些软件,我需要能够创建动态脚本。客户可能会编写脚本,但更有可能的是他们只是修改它们。该脚本将指定这样的内容,

Dataset data = GetDataset("table_name", "field = '1'");

if (data.Read())
{
    string field = data["field"];
    while (cway.Read())
    {
        // do some other stuff
    }
}

因此上面的脚本将从数据库中名为“table_name”的数据库表中读取数据到基于我指定的过滤器“field='1”的某种列表中。它将读取特定字段并执行正常的比较和计算。

最重要的是,这必须是动态的。我可以指定数据库中的任何表、任何过滤器,然后我必须能够访问任何字段。

我正在使用脚本引擎,这意味着我正在编写的脚本必须用 C# 编写。数据集已经过时了,我宁愿远离它们。

只是重申一下,我并不是真的想保留上述格式,我可以在幕后定义任何我想要的方法,供我的 C# 脚本调用。例如,上面的结果可能是这样的,例如,

var data = GetData("table_name", "field = '1'");

while (data.ReadNext())
{
    var value = data.DynamicField;
}

我可以使用反射吗,但也许那会太慢?有什么想法吗?

I have data in a database, and my code is accessing it using LINQ to Entities.

I am writing some software where I need to be able to create a dynamic script. Clients may write the scripts, but it is more likely that they will just modify them. The script will specify stuff like this,

Dataset data = GetDataset("table_name", "field = '1'");

if (data.Read())
{
    string field = data["field"];
    while (cway.Read())
    {
        // do some other stuff
    }
}

So that script above is going to read data from the database table called 'table_name' in the database into a list of some kind based on the filter I have specified 'field='1''. It is going to be reading particular fields and performing normal comparisons and calculations.

The most important thing is that this has to be dynamic. I can specify any table in our database, any filter and I then must be able to access any field.

I am using a script engine that means the script I am writing has to be written in C#. Datasets are outdated and I would rather keep away from them.

Just to re-iterate I am not really wanting to keep with the above format, and I can define any method I want to behind the scenes for my C# script to call. The above could end up like this for instance,

var data = GetData("table_name", "field = '1'");

while (data.ReadNext())
{
    var value = data.DynamicField;
}

Can I use reflection for instance, but perhaps that would be too slow? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2024-10-18 22:55:54

如果您想动态读取 DataReader 上下文,这是一个非常简单的步骤:

ArrayList al = new ArrayList();
SqlDataReader dataReader = myCommand.ExecuteReader();

if (dataReader.HasRows)
{
   while (dataReader.Read())
   {
   string[] fields = new string[datareader.FieldCount];
   for (int i =0; i < datareader.FieldCount; ++i)
   {
      fields[i] = dataReader[i].ToString() ;
   }
   al.Add(fields);
   }

}
这将返回一个由基于读者拥有的字段数量的动态对象组成的数组列表。

If you want to read dynamically a DataReader context, it's a pretty easy step:

ArrayList al = new ArrayList();
SqlDataReader dataReader = myCommand.ExecuteReader();

if (dataReader.HasRows)
{
   while (dataReader.Read())
   {
   string[] fields = new string[datareader.FieldCount];
   for (int i =0; i < datareader.FieldCount; ++i)
   {
      fields[i] = dataReader[i].ToString() ;
   }
   al.Add(fields);
   }

}
This will return an array list composed by a dynamic object based on the number of field the reader has.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文