亚音速 从 msdb 中选择

发布于 2024-09-03 05:08:22 字数 517 浏览 4 评论 0原文

我想使用 Subsonic 执行以下查询:

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

虽然聚合部分对我来说很容易,但问题在于表的名称。我应该如何强制 Subsonic 从不同于默认数据库的数据库中进行选择。

更多详细信息:

这是我在程序中执行此操作的方式:

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

以及我得到的异常:

Need to have at least one From table specified

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-09-10 05:08:22

您尝试过吗:

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

使用它总是一个好主意

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

如果出现问题, 。
queryString 将是一个参数化的 Sql 语句。

编辑:

你真的不应该在 SubSonic 中使用字符串。
您可以使用

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]

从 DAL 获取字符串。
否则,在从数据库中重命名/删除列并重新创建 SubSonic DAL 后,您将不会出现编译时错误。

Edit2:

我刚刚读到您想要查询另一个数据库中的表,而不是用于生成 DAL 的数据库。
我以前从未这样做过,但我想这不起作用的原因是因为 subsonics 的 SqlQuery 类尝试从指定的表名查询模式(以便能够获取限定名称等,失败并吞下异常(或者只是忽略该表)。在构建查询字符串时,您的表不会被包含在内,因为它从未添加到 FromTables 集合中,

但是有一个快速的解决方案应该有效:

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

或者如果您经常需要访问其他数据库,您 可以这样做。甚至可以创建另一个提供程序并在一个项目中使用两个 DAL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文