2017-04-06 115 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

我遇到了代碼問題。提供以下錯誤:找不到列。請幫助 –