2011-01-10 54 views
0

我想显示数据从我的数据库中的列到我的富文本框,但我越来越混合DataSet和DataReader之间 - 我知道下面的大多数代码是正确的,我只是得到两行包含错误,我不知道为什么:ADO.NET - DataRead错误

// 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

什么错误信息? – 2011-01-10 18:51:54

回答

2

您的每个代码段都有问题。

为您提供此数据适配器实现:

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

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

你是不是你的SqlCommand对象与DataAdapter的关联,因此不知道如何来填补你的数据表。

至于你的数据读取器实现,

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

     foreach(DataRow reader in dsRtn) //ERROR 
     { 
      richTextBox = richTextBox.Text + reader[0].ToString(); 
     } 
您使用的是正确的DataReader试试这个

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

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