0

嘿伙计们,我正在使用 C#,我已经成功访问​​了我的数据库并分配了变量等,但我想再次访问它,由于某种原因,它在这一点上失败了:

            OleDbDataReader reader = command.ExecuteReader();

这是可能帮助你们更好地理解的代码片段。如果有人能指出我可能做错了什么,我将不胜感激。

        //#########################
        //   DATABASE OPERATIONS
        //#########################

        // Create the database connections
        string usersConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kronix\Documents\theitguy.accdb");

        OleDbConnection theitguyDBConn = new OleDbConnection(usersConnString);

        //==============================
        //   Populate Customers Table
        //==============================

        try
        {
            // Open theitguy database connection
            theitguyDBConn.Open();

            // Select the fields you want to retrieve from in the database
            string selectString = "SELECT ID, Name, Surname, Address, Town, County, Postcode, HomeNumber, MobileNumber, Email FROM Customers";

            OleDbCommand command = new OleDbCommand(selectString, theitguyDBConn);

            //Send the CommandText to the connection, and then build an OleDbDataReader.
            //Note: The OleDbDataReader is forward-only.
            OleDbDataReader reader = command.ExecuteReader();

            // PROCESS THE DATABASE AND ADD THEM TO THE LISTS FOR USE LATER IN THE PROGRAM
            while (reader.Read())
            {
                custID.Add(reader["ID"].ToString());
                custName.Add(reader["Name"].ToString());
                custSurname.Add(reader["Surname"].ToString());
                custAddress.Add(reader["Address"].ToString());
                custTown.Add(reader["Town"].ToString());
                custCounty.Add(reader["County"].ToString());
                custPostcode.Add(reader["Postcode"].ToString());
                custHomeNumber.Add(reader["HomeNumber"].ToString());
                custMobileNumber.Add(reader["MobileNumber"].ToString());
                custEmail.Add(reader["Email"].ToString());
            }

            // Dispose of the data once used
            reader.Dispose();
            reader.Close();

            // Close the database connection
            theitguyDBConn.Close();

        }
        catch (Exception ex)
        {
            Console.Write("ERROR 201 (Form2): Error reading Customers table in theitguy Database\n");
        }



        //==============================
        //   Populate Repairs Table
        //==============================

        try
        {
            // Open theitguy database connection
            theitguyDBConn.Open();

            // Select the fields you want to retrieve from in the database
            string selectString = "SELECT ID, CustID, Name, Surname, DateIn, Device, Colour, ContactNumber1, ContactNumber2, EstimatedCost, ReportedProblem, Diagnostics, EngineerRemarks, WorkCompleted, PartsUsed, PartsCost, PartsID, Engineer, TotalCost, DateCompleted FROM Repairs";

            OleDbCommand command = new OleDbCommand(selectString, theitguyDBConn);

            //Send the CommandText to the connection, and then build an OleDbDataReader.
            //Note: The OleDbDataReader is forward-only.
            OleDbDataReader reader = command.ExecuteReader();   //###IT'S FAILING HERE!!!###


            // PROCESS THE DATABASE AND ADD THEM TO THE LISTS FOR USE LATER IN THE PROGRAM
            while (reader.Read())
            {
                repID.Add(reader["ID"].ToString());
                repCustID.Add(reader["ID"].ToString());
                repName.Add(reader["ID"].ToString());
                repSurname.Add(reader["ID"].ToString());
                repDateIn.Add(reader["ID"].ToString());
                repDevice.Add(reader["ID"].ToString());
                repColour.Add(reader["ID"].ToString());
                repContactNumber1.Add(reader["ID"].ToString());
                repContactNumber2.Add(reader["ID"].ToString());
                repEstimatedCost.Add(reader["ID"].ToString());
                repReportedProblem.Add(reader["ID"].ToString());
                repDiagnostics.Add(reader["ID"].ToString());
                repEngineerRemarks.Add(reader["ID"].ToString());
                repWorkCompleted.Add(reader["ID"].ToString());
                repPartsUsed.Add(reader["ID"].ToString());
                repPartsCost.Add(reader["ID"].ToString());
                repPartsID.Add(reader["ID"].ToString());
                repEngineer.Add(reader["ID"].ToString());
                repTotalCost.Add(reader["ID"].ToString());
                repDateCompleted.Add(reader["ID"].ToString());
            }

            // Dispose of the data once used
            reader.Dispose();
            reader.Close();

            // Close the database connection
            theitguyDBConn.Close();

        }
        catch (Exception ex)
        {
            Console.Write("ERROR 202 (Form2): Error reading Repairs table in theitguy Database\n");
        }
4

2 回答 2

0

Whoopsie,我发出了嘘声....我发现了我的问题。

我忘了正确命名以下内容:

            repCustID.Add(reader["ID"].ToString());
            repName.Add(reader["ID"].ToString());
            repSurname.Add(reader["ID"].ToString());

应该

            repCustID.Add(reader["CustID"].ToString());
            repName.Add(reader["Name"].ToString());
            repSurname.Add(reader["Surname"].ToString());

傻我。

于 2012-10-07T01:00:50.507 回答
0

Actually that wasn't the only fault...... the REAL PROBLEM was that I was trying to convert currency type in access into a string.

于 2012-10-07T01:29:49.373 回答