运行ExecuteScalar时获取sqlException

时间:2016-01-29 03:23:09

标签: c# sql visual-studio-2013

我使用ExecuteScalar创建了一个从数据库中获取标量的方法,但它抛出异常。 SQL select语句对我来说是正确的。有人可以帮帮我吗。

我得到的确切错误是

  

发票'附近的语法不正确。

代码:

public static String GetTotalBalanceDue() 
{
        decimal totalBalanceDue;

        string selectStatement =
            "SELECT SUM(InvoiceTotal - PaymantTotal - CreditTotal) " +
            "AS BalanceDue FROM Invoices" +
            "WHERE InvoiceTotal - PaymantTotal - CreditTotal > 0";

        try
        {
            using (SqlConnection connection = PayablesDBConnection.GetConnection())
            {
                connection.Open();

                using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection))
                {
                    totalBalanceDue = (decimal)selectCommand.ExecuteScalar();
                }
            }
        }
        catch (SqlException ex)
        {
            //exceptions are thrown to the controller, then to the view
            //Please make sure that do not use MessageBox.Show(ex.Message) in the DAL
            //because it couples the DAL with the view

            //throw is used instead of throw ex because the former preserves the stack trace
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }

        return System.Convert.ToString(totalBalanceDue);
    }

我将select语句修改为:

            string selectStatement =
                "SELECT SUM(InvoiceTotal - PaymentTotal - CreditTotal)" +
                "AS BalanceDue" +
                "FROM Invoices " +
                " WHERE vendorID =" + vendorID;

但我还在

  

发票'附近的语法不正确。

2 个答案:

答案 0 :(得分:1)

这是因为您正在返回记录集,而不是标量

UPDATE:,也因为你的表名和where子句的开头之间没有空格。我相应地更新了这个例子

在您编写查询时,它将返回符合您条件的所有发票。因此,如果您有10张发票和TotalBalanceDue> 0,你将获得10条记录。

如果要获取标量值,则必须将查询约束为一行。我打算用发票ID猜测

string selectStatement =
            "SELECT SUM(InvoiceTotal - PaymantTotal - CreditTotal) " +
            "AS BalanceDue FROM Invoices" +
            " WHERE InvoiceTotal - PaymantTotal - CreditTotal > 0 AND INVOICEID = " + InvoiceId.ToString();

否则,您将不得不更新代码以便能够容纳多行结果

public static String GetTotalBalanceDue() 
    {
        decimal totalBalanceDue;
        DataTable results = new DataTable();
        string selectStatement =
            "SELECT SUM(InvoiceTotal - PaymantTotal - CreditTotal) " +
            "AS BalanceDue FROM Invoices" +
            " WHERE InvoiceTotal - PaymantTotal - CreditTotal > 0";

        try
        {
            using (SqlConnection connection = PayablesDBConnection.GetConnection())
            {
                connection.Open();

                using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection))
                {
                     using(SqlDataAdapter adapter = new SqlDataAdapter(selectCommand))
{
    adapter.Fill(results);
}
                }
            }


        }
        catch (SqlException ex)
        {
            //exceptions are thrown to the controller, then to the view
            //Please make sure that do not use MessageBox.Show(ex.Message) in the DAL
            //because it couples the DAL with the view

            //throw is used instead of throw ex because the former preserves the stack trace
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }

答案 1 :(得分:0)

修改

 string selectStatement =
            "SELECT SUM(InvoiceTotal - PaymantTotal - CreditTotal) " +
            "AS BalanceDue FROM Invoices" +
            "WHERE InvoiceTotal - PaymantTotal - CreditTotal > 0";

TO:

string selectStatement =
            "   SELECT SUM(InvoiceTotal - PaymantTotal - CreditTotal) " +
            "   AS BalanceDue FROM Invoices" +
            "   WHERE InvoiceTotal - PaymantTotal - CreditTotal > 0";