Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
第二次出现Access数据库访问错误?c#_C#_Database_List_Ms Access - Fatal编程技术网

第二次出现Access数据库访问错误?c#

第二次出现Access数据库访问错误?c#,c#,database,list,ms-access,C#,Database,List,Ms Access,嘿,伙计们,我正在使用C#,我已经成功地访问了我的数据库并分配了变量等,但我想再次访问它,由于某种原因,它在这一点上失败了: OleDbDataReader reader = command.ExecuteReader(); 下面是一些代码片段,可以帮助你们更好地理解。如果有人能指出我可能做错了什么,我将不胜感激 //######################### // DATABASE OPERATIONS //

嘿,伙计们,我正在使用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");
        }

哇,我做了一个嘘嘘嘘。。。。我发现了我的问题

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

            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());

我真傻。

事实上,这不是唯一的错。。。。。。真正的问题是,我试图将access中的货币类型转换为字符串。

错误是什么,哪个代码块引发了错误?当您捕获异常时,与其打印一些文本,不如打印错误消息。或者更好,比如ToString()。你可以编辑你的原始问题,而不是像这样发布一个“答案”。