c#中的AVG QUERY

时间:2017-04-06 08:36:55

标签: c# asp.net sql-server

早上好,我正在开发一个代码,可以让我从某个专栏的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;
        }

2 个答案:

答案 0 :(得分:2)

请改用cmd.ExecuteScalar()方法:

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

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

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

修改

您的代码应如下所示:

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:

根据您最近的评论,试试这个

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从数据库中获取单个值。由于您在评论中提到的错误,我建议这样做。

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

USE DB_SACC
GO

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

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

答案 1 :(得分:2)

  • 如果您从数据库中请求单个值,请使用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();
        }
    }
}