Execute SQL with Ar

Source: Internet
Author: User
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 activerecordbasequery

Private _ 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 [])".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.