简体   繁体   English

如何在C#中执行多联接SQL查询

[英]How can I execute a multi Join SQL query in c#

Using IDataReader in c# to pull a list of host names from a database that correspond to a specific business unit, but can't figure out what is wrong. 在C#中使用IDataReader从数据库中提取与特定业务部门相对应的主机名列表,但无法找出问题所在。 I am guessing that there are too many arguments in the SQL statement, or perhaps the ExecuteReader method here should not be used. 我猜想SQL语句中的参数太多,或者也许不应使用ExecuteReader方法。 I cannot create a SP in the target DB so I am left with running the query in some other manner. 我无法在目标数据库中创建SP,因此只能以其他方式运行查询。 Any help will be greatly appreciated. 任何帮助将不胜感激。

    public static IDataReader GetCETHostsList()
    {
        string mySQL = @"SELECT distinct(dbo.Infrastructure.Hostname) dbo.Application_Infrastructure 
                        INNER JOIN dbo.Applications ON dbo.Application_Infrastructure.ID = dbo.Applications.ID
                        INNER JOIN dbo.Infrastructure ON dbo.Application_Infrastructure.InfrastructureID = dbo.Infrastructure.InfrastructureId 
                        WHERE Unit is not null
                        AND dbo.Applications.Unit like '%Terminal%' 
                        AND (dbo.Infrastructure.Hostname like '%ST%' or dbo.Infrastructure.Hostname like '%TR%' )";
        return DatabaseFactory.CreateDatabase("myDB").ExecuteReader(mySQL);
    }

You're missing the FROM keyword. 您缺少FROM关键字。

Generally, if you use SQL embedded in C# programs, it's really a lot easier if you run the SQL in Query Analyzer / SQL Management Studio or something first. 通常,如果您使用嵌入在C#程序中的SQL,那么如果在Query Analyzer / SQL Management Studio中运行SQL或先进行一些操作,实际上会容易得多。 MS SQL Express is free; MS SQL Express是免费的; or VS Pro can run sql queries too. 或VS Pro也可以运行sql查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM