Executing raw sql statement

Posts   
 
    
bernhard
User
Posts: 34
Joined: 15-Jan-2007
# Posted on: 16-Jan-2007 15:14:06   

Hi

Is there anywhere an example which shows how to execute a raw SQL Statement using the DataAccessAdapter (Without using and EntityCollection and RelationPredicateBucket)?

Thanks

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 16-Jan-2007 15:44:19   

Hello,

A solution was given in this thread about using raw sql statement : http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=3809&HighLight=1

But is-it that you really need?Why don't you need to use raw sql statement?

bernhard
User
Posts: 34
Joined: 15-Jan-2007
# Posted on: 16-Jan-2007 16:19:45   

I know that is is not the recommanded way of executing a query. First of all I wanted to use it for testing purposes. It is sometimes easier to write SQL statement than using the LLBL to have it generated and some queries I wanted to test can be generatet the way I need it. (for example subquery in a from clause).

I followed the instruction in the thread but I was not able to execute my sql statement. I thought that it should be possible to get all the necessary classes to (the DbConnection and the DbDataAdapter from the class DataAccessAdapter), but this is not the case.

My code

IDbConnection theConnection = theExtendedDataAccessAdapter.CreateConnection(theExtendedDataAccessAdapter.ConnectionString); DbDataAdapter theDbDataAdapter = theExtendedDataAccessAdapter.CreateDataAdapter();

IDbCommand theCommand = theConnection.CreateCommand(); theCommand.CommandText = "Select * from MyTable";

theDbDataAdapter.SelectCommand = theCommand; // Compile-time error

DataTable table = new DataTable(); theDbDataAdapter.Fill(table);

Now the DbDataAdapter expects a Object of the type DbCommand by I only have a IDbCommand.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 16-Jan-2007 16:31:08   

As DBCommand implements IDBCommand, did you try to cast theCommand in a DBCommand type? Because during the execution, theConnection will be an implemented object of an IDBConnection, so createcommand will create an implemented version of IDBCommand.

bernhard
User
Posts: 34
Joined: 15-Jan-2007
# Posted on: 16-Jan-2007 16:41:16   

Yes I have tried that. My IDBCommand contains an object of the type OracleCommand which can not be casted to an DBCommand.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 17-Jan-2007 00:20:33   

bernhard wrote:

Yes I have tried that. My IDBCommand contains an object of the type OracleCommand which can not be casted to an DBCommand.

Err, an OracleCommand object is a subtype of DbCommand, or am I wrong in this? confused

Frans Bouma | Lead developer LLBLGen Pro
bernhard
User
Posts: 34
Joined: 15-Jan-2007
# Posted on: 17-Jan-2007 08:30:09   

The class Oracle.DataAccess.Client.OracleCommand is not a subtype of DbCommand, it only implements the interface IDbCommand but it does not extend the class, unfortunately.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 19-Jan-2007 08:10:37   

Then the only way to solve this is by using a routine like the storedproc execution routines in the generated code of dataaccessadapter. You should add a similar routine which executes passed in SQL and you're done.

Frans Bouma | Lead developer LLBLGen Pro