ExecuteReader does not return results when a verified request

Consider the following code:

        StringBuilder textResults = new StringBuilder();
        using(SqlConnection connection = new SqlConnection(GetEntityConnectionString()))
        {
            connection.Open();
            m.Connection = connection;
            SqlDataReader results = m.ExecuteReader();
            while (results.Read())
            {
                textResults.Append(String.Format("{0}", results[0]));
            }
        }

I used the Activity Monitor in Sql Server Mgmt Studio in the database to verify the exact request sent. Then I copied this query text to the query editor window in SSMS, and the query returned the expected results. However, it is SqlDataReader resultsalways empty, indicating "listing did not get results."

My suspicion is that somehow the results will not return correctly, which makes me think that something is wrong with the code above, and not with the request itself.

Is there anything that could cause this in the code above? Or something that I forgot?

EDIT:

Here is the request specified by the SQLCommand object:

SELECT DISTINCT StandardId,Number 
FROM vStandardsAndRequirements 
WHERE StandardId IN ('@param1','@param2','@param3') 
ORDER BY StandardId

Here is the query that appears in Activity Monitor:

SELECT DISTINCT StandardId,Number 
FROM vStandardsAndRequirements 
WHERE StandardId IN ('ABC-001-0','ABC-001-0.1','ABC-001-0') 
ORDER BY StandardId

The query works against a single view.

, 3 .

SqlDataReader 0 .

+3
4

Sqldata- sqldatreader.

StringBuilder textResults = new StringBuilder();

        using (var conn = new SqlConnection(GetEntityConnectionString())))
        {
            using (
                var cmd = new SqlCommand(
            "SELECT DISTINCT StandardId,Number" +
                "FROM vStandardsAndRequirements " +
            "WHERE StandardId IN (@param1,@param2,@param3)" +
            "ORDER BY StandardIdl"

       , conn))
            {

                var dSet = new DataSet();
                var dt = new Datatable();

                var da = new SqlDataAdapter(cmd);

                cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value = "ABC-001-0";
        cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = "ABC-001-0.1";
                cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = "ABC-001-0";
                try
                {

                    da.Fill(dSet);

        dt = dSet.Tables[0];

        foreach(Datarow a in dt.Rows)
        {

            textResults.Append(a["StandardId"].tostring()).AppendLine();


        }

        Messabox.Show(textResults.tostring);

                }
                catch (SqlException)
                {
                    throw;
                }

       finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }

            }
        }

.

+1

,

WHERE StandardId IN ('@param1','@param2','@param3') 

?

WHERE StandardId IN (@param1,@param2,@param3) 

, SQLCommand.

+2

,

:

 ... dr = command.ExecuteReader()  ... If dr.Read Then ...

, "dr.Read" , ... "dr", , !

0

Source: https://habr.com/ru/post/1790852/


All Articles