关于亚音速批量插入查询的建议
您好,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您希望能够做类似的事情:
然而这不会起作用,而且说实话,这也不是真正可取的。它不起作用的原因是 SubSonic 不知道将哪些值映射到表中的哪些列。我认为不建议这样做的原因是,对基础表的任何修改都可能很容易导致值被插入到意外的列中,而且实际代码非常不透明。对于对数据库有深入了解的人来说这一切都很好,但是如果您从维护的角度查看该代码,则不可能轻松判断实际发生了什么。
编辑:如果您想要对列名称进行智能感知,您可以执行以下操作(检查 Structs.cs 以查看为您的表/列名称自动生成的内容):
It sounds like you want to be able to do something like:
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):
也许您至少可以“缓存”要插入的列?我忘记了
params string[]
是否会让你这样做。如果是这样,您还可以尝试将表的 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.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)
Subsonic 使用 IActiveRecord 模式,因此如果您使用 T4 模板来生成对象,那么您应该为您的表提供记录类。
在活动记录类上,只需生成一个新类、设置一些属性并调用 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;