【问题标题】:AVG QUERY in c#c#中的平均查询
【发布时间】:2017-04-06 08:36:54
【问题描述】:

早上好,我正在开发一个代码,可以让我在 sql 中退出我的数据库,即某个列的 AVG。 问题是我没有得到它,我认为问题是查询但我不知道如何解决它。 我需要帮助,谢谢。

代码如下:

String connectionString =
       "Data Source=localhost;" +
       "Initial Catalog=DB_SACC;" +
       "User id=sa;" +
       "Password=1234;";

        SqlConnection connection = new SqlConnection(connectionString);

        SqlCommand cmd = new SqlCommand();

        string textt = " USE [DB_SACC] SELECT AVG (Total_Divida) FROM t_pagamentos";

        cmd.CommandText = textt;

        connection.Open();

        cmd.Connection = connection;

        cmd.CommandType = CommandType.Text;

        cmd.ExecuteNonQuery();

        if (textt == null)
        {
            MessageBox.Show("nothing");
        }
        else
        {
            TextBox3.Text = textt;
        }

【问题讨论】:

  • 提示:你正在使用cmd.ExecuteNonQuery()执行查询
  • 使用ExecuteScalar()将其输出存储在某个对象中,然后访问它
  • AVG 不需要 GROUP BY 吗?
  • 我在文本框中放了什么?返回查询值!

标签: c# asp.net sql-server


【解决方案1】:
  • 如果您从数据库中请求单个值,请使用 ExecuteScalar - ExecuteNonQuery 仅返回更新/插入语句中使用的受影响行数
  • USE [DB_SACC] 在您的查询中不是必需的,因为您定义了 "Initial Catalog=DB_SACC;"
  • 添加using 以避免打开连接

代码:

string connectionString =  "Data Source=localhost;Initial Catalog=DB_SACC;User id=sa;Password=1234;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    string textt = "SELECT AVG (Total_Divida) FROM t_pagamentos";
    using (SqlCommand cmd = new SqlCommand(textt, connection))
    {
        connection.Open();
        var result =  cmd.ExecuteScalar(); //write the result into a variable

        if (result == null)
        {
            MessageBox.Show("nothing");
        }
        else
        {
            TextBox3.Text = result.ToString();
        }
    }
}

【讨论】:

  • 我的代码有问题。给出以下错误:找不到列。请帮忙
【解决方案2】:

改用 cmd.ExecuteScalar() 方法:

decimal average = (decimal) cmd.ExecuteScalar();

cmd.ExecuteNonQuery(); 只返回影响的行数,因为您想要读取SELECT 语句的结果集。

我还会从您的 SELECT 语句中删除 USE [DB_SACC],因为您在连接字符串中定义数据库名称。

编辑

您的代码应如下所示:

string textt = "SELECT AVG (Total_Divida) FROM t_pagamentos";
cmd.CommandText = textt;
connection.Open();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
decimal average = (decimal) cmd.ExecuteScalar();
if (textt == null)
{
    MessageBox.Show("nothing");
}
else
{
    TextBox3.Text = average.ToString();
}

编辑 2:

try
{
    string textt = "SELECT AVG (Total_Divida) FROM t_pagamentos";
    cmd.CommandText = textt;
    connection.Open();
    cmd.Connection = connection;
    cmd.CommandType = CommandType.Text;

    decimal average = (decimal)cmd.ExecuteScalar();
    TextBox3.Text = average.ToString();
}
catch(Exception ex)
{
     // log your exception here...
     MessageBox.Show("nothing");
}

编辑 3:

根据你最近的 cmets 试试这个

string connectionString = "Data Source=localhost; Initial Catalog=DB_SACC; User id=sa Password=1234;";
        decimal? average;
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    string textt = "SELECT AVG (Total_Divida) AS 'AVG_DIVIDA' FROM t_pagamentos";
                    cmd.CommandText = textt;
                    connection.Open();
                    cmd.Connection = connection;
                    cmd.CommandType = CommandType.Text;

                    using (DataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            average = decimal.parse(reader["AVG_DIVIDA"].ToString());
                            break;
                        }
                    }
                }
            }


            TextBox3.Text = average.HasValue ? average.ToString() : "Unknown error occurred";

        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to retrieve the average, reason: " + ex.Message);
        }

注意:不推荐使用 DataReader 从数据库中获取单个值。我提出这个是因为你在 cmets 中提到的错误。

如果您遇到任何 SQL 异常,请尝试在 SQL Server 上运行此语句作为独立测试。

USE DB_SACC
GO

SELECT AVG (Total_Divida) AS 'AVG_DIVIDA' FROM t_pagamentos
GO

如果您在执行 T-SQL 语句时仍然遇到任何错误,请将其作为另一个问题发布。

【讨论】:

  • 我的代码有问题。给出以下错误:找不到列。请帮忙
  • 能否在sql server上运行select语句,看看是否有效?
  • 它给出了这个错误:Msg 208, Level 16, State 1, Line 1 Invalid object name 't_pagamentos'
  • 那是你的表,看起来像 sql server 无法找到。尝试在 select 语句之前运行它
  • 使用 db_sacc go;
猜你喜欢
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
相关资源
最近更新 更多