views:

42

answers:

1

Hi,

I want to execute the following query using Subsonic:

SELECT MAX([restore_date]) FROM [msdb].[dbo].[restorehistory]

While the aggregate part is easy for me, the problem is with the name of the table. How should I force Subsonic to select from different database than default one.

More details:

This is the way I do it in my procedure:

SqlQuery query = new Select(Aggregate.Max(@"restore_date",@"restore_date")).From(@"msdb.dbo.restorehistory");
return query.ExecuteScalar<DateTime>();

And the exception I get:

Need to have at least one From table specified
+1  A: 

Have you tried:

DateTime result = new Select(Aggregate.Max(@"restore_date",@"restore_date"))
       .From<RestoreHistory>()
       .ExecuteScalar<DateTime>();

Also it's always a good idea to use

SqlQuery query = new Select(...),
String queryString = query.BuildSqlStatement();

if something goes wrong.
queryString will be a parametrised Sql statement.

Edit:

And your really shouldn't use strings with SubSonic. You can use

RestoreHistory.Columns.RestoreDate  // = restore_date
RestoreHistory.RestoreDateColumn.QualifiedName
// = [msdb].[dbo].[restorehistory].[restore_date] (not sure about this one, just try it out)

And

RestoreHistory.Schema.TableName     // = RestoreHistory
RestoreHistory.Schema.QualifiedName // = [msdb].[dbo].[restorehistory]

to get the strings from the DAL. Otherwise you won't get compiletime errors after renaming / deleting a column from your DB and recreating the SubSonic DAL.

Edit2:

I just read that you want to query a table that is in another database than the one you used for generating your DAL. I have never done that before, but I guess the reason why this won't work is because subsonics' SqlQuery class tries to query the schema from your specified tablename (to be able to get the qualified name etc., fails and swallows the exception (or just ignories the table). While building the querystring, your table is not included because it was never added to the FromTables collection.

But there is a quick solution that should work:

DateTime result = new SubSonic.InlineQuery()
     .ExecuteScalar<DateTime>(
       "SELECT MAX([restore_date]) FROM [msdb].[dbo].[restorehistory]"
      );

Or if your often need to access the other DB, you could even create another provider and work with two DALs' in one project.

SchlaWiener