使用SQL查询获得不一致的结果,需要帮助确定原因

时间:2017-04-21 01:45:05

标签: c# sql-server

我编写了一个简单的SQL查询来计算数据库中表中的记录数。我已成功在MySQL中运行它并且每次都返回4711的计数,这是正确的。当我将其转换为C#并尝试在我的程序中运行它以返回计数以便我可以引用它并显示给我的用户它100%的时间返回0。需要一些帮助来弄清楚我做错了什么。我验证了正确的用户名和密码,并且我已经可以从visual studio成功访问数据库而没有错误,并且看到我试图这样做的表格。

C#代码:

public string SQLDataTotalCalls()
        {
            SqlConnection connection = null;
            try
            {
                var dataSet = new DataSet();

                connection = new SqlConnection("Data Source=databaseservername;Initial Catalog=nameOfDatabase;User Id=myusername;Password=mypassword";
                connection.Open();

                var command = new SqlCommand("SELECT COUNT(SOURCEID) AS 'MYCOUNT' "
                                             + "FROM [databasename].[databasename].[CmsCallHistory] "
                                             +"WHERE disposition = 2 and DISPSPLIT in ('183','186','187','174') AND SEGSTOP BETWEEN '2017-03-22 05:00:00.000' and '2017-03-23 05:00:00.000'",
                    connection)
                {
                    CommandType = CommandType.Text
                };

                var dataAdapter = new SqlDataAdapter {SelectCommand = command};

                dataAdapter.Fill(dataSet);

                return dataSet.Tables[0].Rows[0]["MYCOUNT"].ToString();

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }

        }

原始SQL工作查询,以备不时之需:

SELECT COUNT(SOURCEID) AS 'MYCOUNT'
FROM [databasename].[databasename].[CmsCallHistory]
WHERE disposition = 2 and DISPSPLIT in ('183','186','187','174') AND SEGSTOP BETWEEN '2017-03-22 05:00:00.000' and '2017-03-23 05:00:00.000'

当它在visual studio中运行时我得到0个错误,所以我希望它在认证字符串中只是某种类型的语法错误,但此时我不知道。

4 个答案:

答案 0 :(得分:2)

不应该

Rows[0]["Item"]

Rows[0]["MYCOUNT"]

答案 1 :(得分:1)

var command = new SqlCommand("SELECT COUNT(SOURCEID) AS 'MYCOUNT'\n"
              + "FROM [databasename].[databasename].[CmsCallHistory]\n"
              +"WHERE disposition = 2 and 
                DISPSPLIT in ('183','186','187','174') AND SEGSTOP BETWEEN '2017-03-22 05:00:00.000' and '2017-03-23 05:00:00.000'",
                connection)
            {
                CommandType = CommandType.StoredProcedure
            };

这应该是这样的,因为\ n在SQL查询中是不可读的,因为我尝试过它实际上它与你的语句相连

var command = new SqlCommand("SELECT COUNT(SOURCEID) AS 'MYCOUNT' " //see spacing here
              + "FROM [databasename].[databasename].[CmsCallHistory] "//see spacing here
              +"WHERE disposition = 2 and 
                DISPSPLIT in ('183','186','187','174') AND SEGSTOP BETWEEN '2017-03-22 05:00:00.000' and '2017-03-23 05:00:00.000'",
                connection)
            {
                CommandType = CommandType.Text
            };

答案 2 :(得分:1)

将您的<% if @feed_items.any? %> <table> <% @feed_items.in_groups_of(3).each do |row_feed| %> <tr> <% for feed in row_feed %> <td><%= feed.title %></td> <% end %> </tr> <% end %> </table> <%= will_paginate @feed_items %> <% end %> 更改为CommandType = CommandType.StoredProcedure

答案 3 :(得分:0)

我希望我能给你所有正确的答案,因为你的数据整体帮助了我,可能我将来可能会遇到。但事实证明我试图访问的数据库返回一个0的字符串,因为它实际上是0.该表是在滚动的30天保留策略,恰好在我从sql管理工作室查询数据后立即点击时间在我输入在visual studio中运行Query的方法之前。

再次感谢大家的帮助,问题包含最准确和最新的工作代码。