关于亚音速批量插入查询的建议

发布于 2024-08-11 06:32:10 字数 928 浏览 4 评论 0原文

您好,我正在使用 subsonic 3.0.0.3 和 MVC 运行批量插入,并且我编写了以下示例:

var myquery1 = new Insert(provider).Into<orderitem>("orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total",
                    "orderitem_sessionid", "orderitem_internal", "orderitem_measurement").Values("1", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery1);

        var myquery2 = new Insert(provider).Into<orderitem>("orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total",
                    "orderitem_sessionid", "orderitem_internal", "orderitem_measurement").Values("2", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery2);

        batch.ExecuteTransaction();

这有效并且一切都很好,但是有谁知道我如何才能不列出所有列名称,而是对表中的亚音速等级有一些参考???然后简单地按照正确的顺序排列我的值。

上面的方法有效,但看起来有点不整洁,而且我也有比这个大得多的桌子。

非常感谢

hi am running batch insert with subsonic 3.0.0.3 and MVC and i have wrote the following example:

var myquery1 = new Insert(provider).Into<orderitem>("orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total",
                    "orderitem_sessionid", "orderitem_internal", "orderitem_measurement").Values("1", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery1);

        var myquery2 = new Insert(provider).Into<orderitem>("orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total",
                    "orderitem_sessionid", "orderitem_internal", "orderitem_measurement").Values("2", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery2);

        batch.ExecuteTransaction();

this works and all is fine, however does anyone know how i can get away with not listing all the column names, but instead have some reference to the subsonic class for the table ???? and then simply have my values in the correct order.

the above works but seems a bit untidy, and i have much bigger tables than this too.

many thanks

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

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

发布评论

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

评论(3

于我来说 2024-08-18 06:32:10

听起来您希望能够做类似的事情:

var myquery1 = new Insert(provider).Into<orderitem>().Values("1", "1", "1", "0.00", "12345", "0", "1x1");

batch.QueueForTransaction(myquery1);

然而这不会起作用,而且说实话,这也不是真正可取的。它不起作用的原因是 SubSonic 不知道将哪些值映射到表中的哪些列。我认为不建议这样做的原因是,对基础表的任何修改都可能很容易导致值被插入到意外的列中,而且实际代码非常不透明。对于对数据库有深入了解的人来说这一切都很好,但是如果您从维护的角度查看该代码,则不可能轻松判断实际发生了什么。

编辑:如果您想要对列名称进行智能感知,您可以执行以下操作(检查 Structs.cs 以查看为您的表/列名称自动生成的内容):

var myquery1 = new Insert(provider).Into<orderitem>(OrderItemTable.ProductIdColumn).Values("1");

batch.QueueForTransaction(myquery1);

It sounds like you want to be able to do something like:

var myquery1 = new Insert(provider).Into<orderitem>().Values("1", "1", "1", "0.00", "12345", "0", "1x1");

batch.QueueForTransaction(myquery1);

However this won't work and to be honest it's not really advisable either. The reason it won't work is that SubSonic won't know which values to map to which columns in your table. The reason I don't believe it's advisable is that any modification to the underlying tables could easily result in the values being inserted into unexpected columns and also the actual code is very opaque. It's all very well for someone with an intimate knowledge of the database but if you look at that code from a maintenance perspective it's impossible to easily tell what is actually happening.

EDIT: If you want intellisense for your column names you can do the following (check Structs.cs to see what gets generated automatically for your table/column names):

var myquery1 = new Insert(provider).Into<orderitem>(OrderItemTable.ProductIdColumn).Values("1");

batch.QueueForTransaction(myquery1);
荒路情人 2024-08-18 06:32:10

也许您至少可以“缓存”要插入的列?我忘记了 params string[] 是否会让你这样做。

string[] columnlist = new string[] {"orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total", "orderitem_sessionid", "orderitem_internal", "orderitem_measurement"};
var myquery1 = new Insert(provider).Into<orderitem>(columnList).Values("1", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery1);

        var myquery2 = new Insert(provider).Into<orderitem>(columnList).Values("2", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery2);

        batch.ExecuteTransaction();

如果是这样,您还可以尝试将表的 Columns 结构转换为字符串数组,但我不知道哪个 3.0 模板会生成该数组(如果有)。 (仍然喜欢 2.2,还没准备好重新开始 3.0)

Maybe you could at least "cache" the columns you want to insert to? I forget if params string[] will let you do that or not.

string[] columnlist = new string[] {"orderitem_orderid", "orderitem_productid", "orderitem_qty", "orderitem_total", "orderitem_sessionid", "orderitem_internal", "orderitem_measurement"};
var myquery1 = new Insert(provider).Into<orderitem>(columnList).Values("1", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery1);

        var myquery2 = new Insert(provider).Into<orderitem>(columnList).Values("2", "1", "1", "0.00", "12345", "0", "1x1");
        batch.QueueForTransaction(myquery2);

        batch.ExecuteTransaction();

If it does, you could also try converting the Columns struct for the table to a string array, but I don't know which 3.0 templates would generate that if any. (Still loving 2.2 and not ready to start over with 3.0)

漫雪独思 2024-08-18 06:32:10

Subsonic 使用 IActiveRecord 模式,因此如果您使用 T4 模板来生成对象,那么您应该为您的表提供记录类。

在活动记录类上,只需生成一个新类、设置一些属性并调用 Add() 方法即可以更简洁的方式有效地完成您正在做的事情。

只需确保您的表上设置了主键,您的列命名合理,并且只是“正常工作”为你

像这样的东西;

OrderItem item = new OrderItem();
item.Total = 24.32;
item.Qty = 3;
item.ItemID = 23;
item.CreatedBy = 'bjones';
item.Add();

Subsonic uses the IActiveRecord pattern so if you used the T4 templates to gen objects for you then you should have classes for records for your tables.

On the active record classes its simple to just gen up a new class, set some properties and call the Add() method to effectively do what you doing in a much more concise manner.

Just make sure you have a Primary Key set on your table, that your columns are named sanely and things just "just work" for you.

Something like;

OrderItem item = new OrderItem();
item.Total = 24.32;
item.Qty = 3;
item.ItemID = 23;
item.CreatedBy = 'bjones';
item.Add();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文