2013-07-16 3 views
0

데이터베이스에 두 개의 테이블이 있습니다. 학생과 계정. 내 응용 프로그램에서 reader.GetDecimal() 메서드를 사용하여 여러 테이블에서 데이터를 읽는 데 문제가 있고 두 번째 테이블에서 데이터를 가져 오는 데 문제가 있습니다.reader.GetDecimal()을 별도의 테이블에있는 열에 사용하는 방법은 무엇입니까?

GetDecimal 메서드를 사용하여이 작업을 수행 할 수 있습니까? 또는 계정 테이블에서 필요한 것을 얻으려면 쿼리 중 하나에 추가해야합니까?

데이터베이스 테이블 :

계정

Accounts

학생 Student

코드 :

  //Query Student table for bAlertSetup 
      SqlCeCommand AlertQuery = new SqlCeCommand("SELECT * from Students AND Accounts", conn); 
      reader = AlertQuery.ExecuteReader(); 

      while (reader.Read()) 
      { 
       bSinglePersonAlertSetup = reader.GetBoolean(5); 

       if (bSinglePersonAlertSetup == true) 
       { 
        int AccountID = reader.GetInt32(7); 
        decimal Threshold = reader.GetDecimal(6); 
        //decimal Total = get decimal from the accounts table where accountID (in the accounts table) = AlertAccountID 

        //See if Students account is below the defined threshold 
        if (Total < Threshold) 
        { 
         StudentEmailAddress = reader.GetString(3); 

         if (StudentEmailAddress != null) 
         { 
          Console.WriteLine(StudentEmailAddress); 
          mail.To.Add(StudentEmailAddress); 

          //Update bAlertSetup 
          SqlCeCommand UpdateBool = new SqlCeCommand("UPDATE Students set bSendAlert = 0 WHERE UserId = @ID"); 
          UpdateBool.Parameters.AddWithValue("@ID", reader.GetInt32(0)); 
          UpdateBool.Connection = conn; 
          UpdateBool.ExecuteNonQuery(); 
         } 
        }  
       } 

편집 : 여기

내가 필요한 열이 올바르게 두 테이블을 조인 믿고 사용하고있는 새 쿼리입니다. 이제 GetDecimal() 메서드에서 사용할 각 열의 인덱스를 얻으려면 어떻게해야합니까?

SqlCeCommand AlertQuery = new SqlCeCommand("SELECT st.bAlertSetup, st.AccountThreshold, st.AlertAccountID, acc.AccountID, acc.AccountTotal FROM Students st INNER JOIN Accounts acc ON st.AlertAccountID = acc.AccountID", conn); 
+0

이 시도 도움이 될 것입니다 사용할 수 있습니다 read.GetDecimal (6)로 .toString()를. – Marek

+0

두 번째 테이블에서 열을 가져 오려면 여기에서 쿼리를 편집해야합니다. – Ehsan

+0

나는 그 부분이 현재 어떻게 올바르게 작동한다고 믿는다. 내가 겪고있는 문제는 계정 테이블에서 해당 "AccountID"및 "AccountTotal"을 얻는 것입니다. –

답변

1
SqlCeCommand AlertQuery = new SqlCeCommand("SELECT S.*,A.AccountTotal from Students S Inner Join Accounts A ON S.AlertAccountID = A.AccountID"); 

SQL에서 쿼리를 실행해야합니다 ...이 같은 이름으로 다음 AccountTotal를 얻을 수 을 제공하거나 각 필드 쉼표에 대해 S. *를 S. [FieldName]으로 대체하고 각 열의 순서가 항상 쿼리에 보존된다는 것을 알고 있어야합니다.

+0

S.AccountThreshold, S.AlertAccountID와 같은 쿼리에 원하는 각 열을 정의하면됩니다. GetDeimal() 메서드에 사용하는 인덱스는 쿼리에 나열한 순서와 일치합니까? 그래서 GetDecimal (0)을 사용하여 AccountThreshold를 찾으십니까? –

+1

S.AccountThreshold가 쿼리에서 첫 번째로 오면 인덱스 0에 –

+0

완벽하게 작동합니다. 고마워요! –

1

쿼리에 두 테이블을 올바르게 조인하고 필요한 개별 열만 선택해야합니다. 그래서 쿼리는 ... 다음과 같은 수 있습니다

SELECT st.UserID, st.FirstName, st.LastName, acc.AccountName, acc.AccountTotal 
FROM Student st 
JOIN Accounts acc 
ON st.AlertAccountID = acc.AccountID 

당신은

decimal total = reader.GetDecimal("AccountTotal"); 
+0

컴파일 할 때 문자열을 int로 변환 할 수 없습니다. 열 이름에서 테이블의 색인을 가져 오는 방법이 있습니까? –