2011-01-10 4 views
0

내 데이터베이스의 열에서 풍부한 텍스트 상자에 데이터를 표시하려고하는데 DataSet과 DataReader가 섞여 있습니다. 아래 코드의 대부분이 정확합니다. 오류가 포함 된 두 줄을 얻었습니다. 이유는 확실하지 않습니다.ADO.NET - 데이터 읽기 오류

// Create a connection string 
      string ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harley\\Desktop\\Test.accdb"); 
      string SQL = "SELECT * FROM Paragraph"; 

      // create a connection object 
      SqlConnection conn = new SqlConnection(ConnectionString); 

      // Create a command object 
      SqlCommand cmd = new SqlCommand(SQL, conn); 
      conn.Open(); 

      DataTable dt = new DataTable(); 
      da.Fill(dt); //ERROR 

      // Call ExecuteReader to return a DataReader 
      SqlDataReader reader = cmd.ExecuteReader(); 

      foreach(DataRow reader in dsRtn) //ERROR 
      { 
       richTextBox = richTextBox.Text + reader[0].ToString(); 
      } 

      //Release resources 
      reader.Close(); 
      conn.Close(); 

     } 
+0

오류 메시지가 무엇입니까? –

답변

2

각 조각에 문제가 있습니다. 당신이 제공하는 데이터 어댑터 구현을 위해

:

 SqlCommand cmd = new SqlCommand(SQL, conn); 
     conn.Open(); 

     DataTable dt = new DataTable(); 
     da.Fill(dt); //ERROR 

그것은 당신의 DataTable을 채우는 방법 아무 생각이 없다, 그래서 당신은 당신의 데이터 어댑터와 함께 SqlCommand 개체를 연결하지 않습니다. 당신의 데이터 판독기 구현으로

, 잘못이를 시도하여 DataReader를 사용하는

 // Call ExecuteReader to return a DataReader 
     SqlDataReader reader = cmd.ExecuteReader(); 

     foreach(DataRow reader in dsRtn) //ERROR 
     { 
      richTextBox = richTextBox.Text + reader[0].ToString(); 
     } 

:

 // Call ExecuteReader to return a DataReader 
     SqlDataReader reader = cmd.ExecuteReader(); 

     while(reader.Read()) 
     { 
      richTextBox = richTextBox.Text + reader[0].ToString(); 
     }