如何使用 Microsoft Visual Studio 将查询插入到 C# 中的另一个查询中?

Posted

技术标签:

【中文标题】如何使用 Microsoft Visual Studio 将查询插入到 C# 中的另一个查询中?【英文标题】:How to insert a query into another query in C# using Microsoft Visual Studio? 【发布时间】:2015-06-08 02:16:55 【问题描述】:

是否可以在 C# 中将查询插入到另一个查询中?目前我正在连接到一个访问数据库,当我将查询插入到 Microsoft Access 上 SQL 视图中的另一个查询中时,会显示我想要的数据。但是,当我将相同的代码插入 C# 时,查询带有下划线。基本上,此查询是将“日”视图中生成的查询中的数据包含到“月”视图的查询中的 FROM 部分。

如何修改查询以便能够在 C# 中运行?

Microsoft Access 上 SQL 视图中“日视图”的查询是:

SELECT outbound.[Receipt date], outbound.[Receive plant], Avg(outbound.[Aging Days]) AS [AvgOfAging Days] FROM outbound GROUP BY outbound.[Receipt date], outbound.[Receive plant] HAVING (((outbound.[Receipt date]) Is Not Null));

Microsoft Access 上 SQL 视图中“月视图”的查询是:

SELECT Format([Receipt date],"mm/yyyy") AS [Month], Avg(Day.[AvgOfAging Days]) AS [AvgOfAvgOfAging Days] FROM  (SELECT outbound.[Receipt date], outbound.[Receive plant], Avg(outbound.[Aging Days]) AS [AvgOfAging Days] FROM outbound WHERE (outbound.[Receipt date] > #4/1/2015#) GROUP BY outbound.[Receipt date], outbound.[Receive plant] HAVING (((outbound.[Receipt date]) Is Not Null))) AS [Day] WHERE Day.[Receive plant] = "2300" GROUP BY Format([Receipt date],"mm/yyyy"), Day.[Receive plant] HAVING (((Format([Receipt date],"mm/yyyy")) Is Not Null));

(对于日期和工厂,我有硬编码值来测试查询,但我的实际程序应该读取用户选择)

为适应“月”视图的 C# 应用程序而修改的代码是:

string cmdQuery = "SELECT Format([Receipt date],\"mm/yyyy\") AS [Month], Avg(Day.[AvgOfAging Days]) AS [AvgOfAvgOfAging Days] "
                                + "FROM ("SELECT outbound.[Receipt date], outbound.[Receive plant], Avg(outbound.[Aging Days]) AS [AvgOfAging Days] " + "FROM outbound " + "WHERE ([Receipt date] >= #" + dateFrom + "#) AND ([Receipt date] <= #" + dateTo + "#) AND ((outbound.[Receive plant]) ='" + plant[i] + "')" + "GROUP BY outbound.[Receipt date], outbound.[Receive plant] " + "HAVING (((outbound.[Receipt date]) Is Not Null)); ) AS [Day] "

                                + "WHERE ((Day.[Receive plant]) ='" + plant[i] + "') "
                                + "GROUP BY Format([Receipt date],\"mm/yyyy\"), Day.[Receive plant] "
                                + "HAVING (((Format([Receipt date],\"mm/yyyy\")) Is Not Null)) ";

如何编辑上面的查询以使该查询能够在 C# 中运行?

谢谢。

【问题讨论】:

【参考方案1】:

    切换到参数化值/占位符(SQL 命令参数);然后

    使用string.Format插入子查询,子查询本身应该是一个有效查询。

只需将内部参数添加到最终 SQL 命令对象,以及外部查询的任何参数。也就是说,字符串构建针对查询体,不针对任何数据。

为了让生活更轻松,内部查询也应该位于一个用括号括起来的派生表位置,并给定一个别名。

例如:

var innerQuery = "SELECT .. WHERE name = @name";
var finalQuery = string.Format("SELECT * FROM (0) AS T ..", innerQuery);

var cmd = conn.NewCommand();
cmd.CommandText = finalQuery;
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = "Fred";

如果遵循这种方法,那么唯一需要注意的是避免名称冲突,如果定义(并遵循)相关规则,这很容易。


或者,使用允许以类型安全的方式构造查询(包括嵌入式查询)的众多 ORM/Mappers/LINQ 提供程序之一。

如果继续以这种方式编写原始 SQL,还可以考虑使用verbatim string literals。

【讨论】:

【参考方案2】:

我认为这会起作用 - 但我强烈建议使用更易于维护的方式...

  string cmdQuery = "SELECT Format([Receipt date],\"mm/yyyy\") AS [Month], Avg(Day.[AvgOfAging Days]) AS [AvgOfAvgOfAging Days] "
                            + "FROM ( "+ "SELECT outbound.[Receipt date], outbound.[Receive plant], Avg(outbound.[Aging Days]) AS [AvgOfAging Days] " + "FROM outbound " + "WHERE ([Receipt date] >= #" + dateFrom + "#) AND ([Receipt date] <= #" + dateTo + "#) AND ((outbound.[Receive plant]) ='" + plant[i] + "')" + "GROUP BY outbound.[Receipt date], outbound.[Receive plant] " + "HAVING (((outbound.[Receipt date]) Is Not Null)); ) AS [Day] "

                            + "WHERE ((Day.[Receive plant]) ='" + plant[i] + "') "
                            + "GROUP BY Format([Receipt date],\"mm/yyyy\"), Day.[Receive plant] "
                            + "HAVING (((Format([Receipt date],\"mm/yyyy\")) Is Not Null)) ";

【讨论】:

上面的查询不再加下划线,但是,当我运行应用程序时,FROM 子句中似乎存在语法错误。 查看@user2864740 的回答 - 你拥有的东西太难以管理了。

以上是关于如何使用 Microsoft Visual Studio 将查询插入到 C# 中的另一个查询中?的主要内容,如果未能解决你的问题,请参考以下文章

如何下载microsoft visual studio

如何使用 Microsoft Visual C++ 2008 编译 PHP 扩展?

如何编写代码以使用 microsoft visual c++ 调用 JNI [关闭]

Visual Stu dio(vs)2015企业版有没有?

如何在 C++ 中使用 Microsoft Visual Studio 2012 查找语法错误?

如何使用 Microsoft Visual C++ 调试器调试 MinGW EXE?