尝试将数据从Microsoft Access 2010提取到C#程序

时间:2015-11-23 00:08:59

标签: c# sql ms-access

我正在尝试构建一个程序,程序从访问数据库中提取数据。这是我构建的SQL语句(已经在Access中测试过并且有效):

SELECT Volunteer.LoaneeID, FirstName, LastName, SUM(HoursWorked )
FROM VOLUNTEER 
INNER JOIN PERSON 
    ON Volunteer.LoaneeID = Person.ID 
Group by FirstName, LastName, LoaneeID;

我遇到的问题是上面的总结陈述。当我运行我的代码时,我得到出现意外问题:“HoursWorked”。

这是我构建的代码。我确信这是一个愚蠢的错误。谢谢你的帮助!

Private void more15(string sql)
    {
        //string inputLine;
        clsQuery loanee;

        mLoanee.Clear();

        // Read the data from the specified file.

        if (File.Exists(mClientFile) == false)
        {
            ShowMessage(mClientFile + " does not exist. Please open another file.");
            return;
        }
        try
        {
            openDatabaseConnection();
            mDB.Open();
            OleDbCommand cmd;
            OleDbDataReader rdr;
            cmd = new OleDbCommand(sql, mDB);
            rdr = cmd.ExecuteReader();

            while (rdr.Read() == true)
            {
                loanee = new clsQuery(
                          (int)rdr["LoaneeID"],
                          (string)rdr["FirstName"],
                          (string)rdr["LastName"],
                          (int)rdr["HoursWorked"]);

                mLoanee.Add(loanee);
            }
            rdr.Close();
        }
        catch (Exception ex)
        {
            ShowMessage("There was an unexpected problem: " + ex.Message);
        }
        finally
        {
            closeDatabaseConnection();
        }
    }

1 个答案:

答案 0 :(得分:0)

您没有为sum(HoursWorked)提供别名。

更改您的查询,如下所示:

SELECT Volunteer.LoaneeID, FirstName, LastName, SUM(HoursWorked ) as HoursWorked 
FROM VOLUNTEER 
INNER JOIN PERSON 
ON Volunteer.LoaneeID = Person.ID 
Group by FirstName, LastName, LoaneeID;