views:

339

answers:

3

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

A: 

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();
CmdrTallen
i know how to add a record, and i also know how subsonic works and what it creates, thanks for the info but it dont really have anything to do with my querry as im doing a batch insert of many records in one go, the method above is not really recommended for batch inserts thats why rob created the batch process !!!
minus4
+1  A: 

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);
Adam
true nice point i did find a longer way of doing it lol with orderitemTable.columnname all good point but i was thinking more on the lines of new Insert(provider).Into<orderitem>(orderitem.columns.all).Values("1", "1", "1", "0.00", "12345", "0", "1x1"); or simular intelisense says params expression <func<orderitem,object>>[] props : insert ????
minus4
+1  A: 

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)

ranomore
3.0.0.3 is great, but not much documentation so you have to work it all out yourself :-) in a way i kind of like it this way as you get to learn and use it better !!! the above yeah its possible, i thought maybe in the generated classes i could put an array of columns and be able to just call that anywhere... in theory the TT files could be modded to generate this LIST !!!!! thinking wonder if thats possible anyway :-) customersTable.Columns.ToArray lol
minus4