From: http://forum.castleproject.org/viewtopic.php? T = 52
Hi, I got somewhat the same problem. I need to run T-SQL commands, as we are new to activerecord and nhib.pdf, and wowould like a quick "workaround" when we can't find our ways
I refered to http://castleproject.org/index.php/ActiveRecord:Using_HQL and made a derived class from activerecordbasequery.
Code: |
Public class sqlnonquery Inherits activerecordbasequeryPrivate _ commandtext as string Public property commandtext () as string Get Return _ commandtext End get Set (byval value as string) _ Commandtext = Value End set End Property Public sub new (byval commandtext as string) Mybase. New (GetType (User )) _ Commandtext = commandtext End sub Public overrides function execute (byval session as nhibib. isession) as object Dim cmd as idbcommand = session. Connection. createcommand () Cmd. commandtext = commandtext Cmd. executenonquery () Return nothing End Function End Class '''Usage ''' Dim Q As sqlnonquery ("any SQL statement ") Executequery (q) |
But still have the same problem as Nick, need to pass some arbitrary activerecord class type for the activerecordbasequery constructor.
If you have better ways, Please advice... thanks.
OK, I figured out something that works:
Code: |
Isessionfactoryholder holder = activerecordmediator. getsessionfactoryholder (); Isession session = holder. createsession (typeof (User ));Idbcommand cmd = session. Connection. createcommand (); Cmd. commandtext = "alter table stops alter description type text ;"; Cmd. executenonquery ();
Holder. releasesession (session ); |
However it's a lot of code, and two things that bug me are:
1) must give createsession some arbitrary activerecord class type (type "user" in the example)
2) getsessionfactoryholder () seems to be a "temporary" method, not intended to be used
This disscuss is old though, I post the idea to solve abve problem. It maybe help someone who has same problem.
Code: |
// Create Query Simplequery <object []> query = new simplequery <object []> ( Typeof (topcalled ), @ "Select recid, count (called_number) as frequency, called_number From detail_record Group by called_number Order by frequency DESC ");// Execute Query Object [] [] Results = query. Execute (); |
It also might work that you use topcalled class instead of "object []" if the class has "operator = (object [])".