SqlDataReader不返回任何数据,但查询有效

时间:2018-07-27 06:39:46

标签: c# sql sql-server sqldatareader

我从MSSQL数据库提取数据时遇到问题。当我使用SQL Studio编写语句时,它可以正常工作并返回500多个记录,但读者结果始终为空。

这是我的代码atm。

internal static void GetEntities(EntityRequest request, ref EntityResponse response)
    {
        string connString = @"server=DESKTOP-MCPU420\SQLEXPRESS;Initial Catalog=Skyline;Integrated Security=True";
        try
        {
            using (SqlConnection sqlConn = new SqlConnection(connString))
            {

                string query = null;
                if (request.Expression.OrderBy == null)
                    query = string.Format("SELECT {0} FROM {1} WHERE {2};",
                        request.Expression.Columns, request.Expression.TableName, request.Expression.Where);
                else
                    query = string.Format("SELECT {0} FROM {1} WHERE {2} ORDER BY {3};",
                        request.Expression.Columns, request.Expression.TableName, request.Expression.Where, request.Expression.OrderBy);

                using (SqlCommand command = new SqlCommand(query, sqlConn))
                {
                    sqlConn.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                            response.Content.Add((IDataRecord)reader);
                    }
                }
            }

        }
        catch (Exception e)
        {
            ErrorMessageProvider.SetResponseMessage(ref response, ErrorList.PROGRAM_FAILED, request.Language);
            response.ErrorMessage = e.ToString();
        }
    }

此查询进入:

SELECT Id
    ,IATACode
    ,AgentName
    ,CASSCode
    ,HomeAirport_Id
FROM dbo.CLI_Agent
WHERE IATACode LIKE '%'
    AND AgentName LIKE '%'
    AND CASSCode LIKE '%';

SQL Studio的结果 Link to image

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

连接字符串有一个小问题。我修复了它,现在可以了。它总是归结为各种错误:-)

新连接。字符串:

string connString = @"Data Source=DESKTOP-MCPU420\SQLEXPRESS;Initial Catalog=Skyline;Integrated Security=True;";