当检查的查询返回结果时,ExecuteReader 不返回结果

发布于 2024-10-16 09:33:20 字数 1224 浏览 0 评论 0原文

考虑以下代码:

        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]));
            }
        }

我在数据库上使用 Sql Server Mgmt Studio 中的活动监视器来检查正在发送的确切查询。然后,我将该查询文本复制到 SSMS 中的查询编辑器窗口,查询返回了预期结果。但是,SqlDataReader results 始终为空,表示“枚举未返回任何结果”。

我的怀疑是,不知何故结果没有正确返回,这让我认为上面的代码有问题,而不是传递的查询本身有问题。

上面的代码中有什么会导致这个问题吗?还是我忽略了什么?

编辑:

这是 SQLCommand 对象指示的查询:

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

这是出现在活动监视器中的查询:

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

该查询针对单个视图进行操作。

当我对数据库运行第二个查询时,它返回 3 行。

SqlDataReader 指示 0 行。

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 Activity Monitor within Sql Server Mgmt Studio on the database to inspect the exact query that was being sent. I then copied that query text to a query editor window within SSMS, and the query returned the expected results. However, SqlDataReader results is always empty, indicating "The enumeration returned no results."

My suspicion is that somehow the results are not being returned correctly, which makes me think there's something wrong with the code above, and not the query itself being passed.

Is there anything that would cause this in the code above? Or something I've overlooked?

EDIT:

Here is the query as indicated by the SQLCommand object:

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

Here is the query as it 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 is working against a single view.

When I ran the second query against the database, it returned 3 rows.

The SqlDataReader indicates 0 rows.

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

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

发布评论

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

评论(4

楠木可依 2024-10-23 09:33:20

尝试使用 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();
                }

            }
        }

问候。

try to use Sqldata adapter instead of 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();
                }

            }
        }

Regards.

余生再见 2024-10-23 09:33:20

你确定是它

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

而不是这个吗?

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

参数不应加引号,也不应位于 SQLCommand 对象中。

Are you sure it is

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

instead of this?

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

Parameters should not be quoted, not in the SQLCommand object.

海之角 2024-10-23 09:33:20

我观察到了非常好的行为,

我在代码中查找错误:

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

并发现“dr.Read”工作正常,但是......
当我将鼠标悬停在“dr”上查找数据时,返回值消失了!

Very nice behavior I've observed

I looked for errors in code:

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

and found that 'dr.Read' works fine, but...
when I mouseover on 'dr', to lookup for data, return values disappeared !

情何以堪。 2024-10-23 09:33:20

检查您的连接字符串并确保您没有作为用户实例进行连接。

http://msdn.microsoft.com/en-us/library/ms254504.aspx

Check your connection string and make sure you are not connecting as a user instance.

http://msdn.microsoft.com/en-us/library/ms254504.aspx

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