2017-04-06 56 views
1

早上好,我正在开发一个代码,可以让我在sql中查看某个列的AVG。 问题是我没有得到它,我认为问题是查询,但我不知道如何解决它。 我需要帮助,谢谢。c#中的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; 
     } 
+1

提示:你正在执行一个查询'cmd.ExecuteNonQuery()' –

+0

http://stackoverflow.com/questions/5349114/executenonquery – mayk

+2

使用'ExecuteScalar()'将其存储在某个对象中,然后访问它 –

回答

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:

在你最近的评论光试试这个

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语句时仍然遇到任何错误,请将其作为另一个问题发布。

+0

完全一样,谢谢。 –

+0

谢谢@fubo指出这一点! –

+0

我遇到了代码问题。提供以下错误:找不到列。请帮忙 –

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(); 
     } 
    } 
} 
+0

我遇到了代码问题。提供以下错误:找不到列。请帮助 –