2012-10-07 4 views
0

나는 C#을 사용하고 있는데이 성공적으로 내 데이터베이스를 액세스 및 변수 등을 할당하지만 난 그것을 두 번 액세스하려는 어떤 이유로 든이 시점에서 실패있어 한 얘들 아 :데이터베이스 액세스 오류에 두 번째로 액세스합니까? 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"); 
     } 
+1

어떤 오류가 있고 어떤 코드 블록이 오류를 던지고 있습니까? – codingbiz

+0

예외를 잡을 때 일부 텍스트를 인쇄하는 대신 오류 메시지를 인쇄하지 않는 것이 좋습니다. 또는 더 나은 아직, ex.ToString(). 그러면 문제가 어디서 어디서 발생했는지 정확하게 알 수 있습니다. –

답변

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

바보 나이어야한다.

0

사실 그게 유일한 잘못이 아니 었습니다. 실제 문제는 통화 유형을 액세스로 변환하려고 시도했기 때문입니다.

+0

이와 같이 "답변"을 게시하지 않고 원래 질문을 편집 할 수 있습니다. –

관련 문제