-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to execute 'normal' SQL queries
PostPosted: Sat Jun 23, 2007 5:44 pm 
Newbie

Joined: Sat Jun 23, 2007 5:36 pm
Posts: 7
I've got a question about executing 'normal' SQL queries. In the project I'm working on I've got two kind of situations which are done in a qay, that is not using NHibernate. I would like to do this with NHibernate, but I'm not sure how to do this:

1) Execute an update statement without having to send the objects, so a query like this:

UPDATE table WHERE removed = 0

2) There are cases where an insert has to be made based on data from 3 different tables. Doing this in 1 query is way more efficient than doing it all in code en save those objects using NHibernate. This would be a query like:

INSERT INTO table(a,b,c)
SELECT .......

I hope these two cases are possible in NHibernate, so I can use NHibernate for all the functionality. I couldn't find it, maybe I looked wrong. It would be a big help if someone could tell me how to do this, or where to find how to do this.

I'm using the last version of NHibernate.

Thanks in advance for your help.
Koen Willemse


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 24, 2007 2:05 pm 
Newbie

Joined: Sat Jun 09, 2007 9:40 am
Posts: 11
I think you are looking for this:

IDbConnection cn = session.Connection;

You now have a normal ADO.Net connection.

Code:
            IDbConnection cn = session.Connection;
            IDbCommand dbCmd = cn.CreateCommand();
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.CommandText = "Insert_Bank_Transaction";

            SqlParameter parameter = null;

            parameter = new SqlParameter("@data_source_code", SqlDbType.VarChar);
            parameter.Value = DataSourceCode;
            parameter.Direction = ParameterDirection.Input;
            dbCmd.Parameters.Add(parameter);



            IDbTransaction tx = cn.BeginTransaction();
            dbCmd.Connection = cn;
            dbCmd.Transaction = tx;
            dbCmd.ExecuteNonQuery();
            tx.Commit();



See also:

13.3. Custom SQL for create, update and delete

Being somewhat new to NH I've found that to ease the learning curve and still be productive I'm falling back to the IDbconnection with which I'm familiar. As I become more versant with NH coding I'll start using more of it's features.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 24, 2007 4:16 pm 
Newbie

Joined: Sat Jun 23, 2007 5:36 pm
Posts: 7
Thank you, this was very helpfull, this was what I was looking for. Just one more thing. I see that you use the @ sign as the parametertoken. Is this token replaced for a token that the database accepts. I know that MySQL has a ? as parameter token. Does anybody know this?

Thank you very much.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 25, 2007 10:03 am 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
KoenWillemse wrote:
Thank you, this was very helpfull, this was what I was looking for. Just one more thing. I see that you use the @ sign as the parametertoken. Is this token replaced for a token that the database accepts. I know that MySQL has a ? as parameter token. Does anybody know this?

Thank you very much.
In SQL Server, you can use 'named parameters', just like Richard12345 displays. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.