2012-10-15 20 views
1

我正在使用嵌套中繼器。我的問題似乎是在目前,當我執行我的SQL命令,沒有數據返回給數據讀取器。即使我運行完全相同的查詢(複製並粘貼)到SQL Server中。SQL命令未填充數據讀取器

我的noteDrClient閱讀器不包含數據,但它確實知道表中有5列。我不知道現在要做什麼或者爲什麼沒有數據傳入數據讀取器。任何人都可以看到明顯的問題?

SqlConnection con = new SqlConnection("Data Source=***;Initial Catalog=*;User ID=*;Password=*;Integrated Security=False;MultipleActiveResultSets=true;"); 

上面是我的連接字符串。請注意,我將多個活動結果集設置爲true。我這樣做是因爲我的數據閱讀器一直處於打開狀態,儘管它已關閉。

protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     RepeaterItem item = e.Item; 
     if ((item.ItemType == ListItemType.Item) || 
      (item.ItemType == ListItemType.AlternatingItem)) 
     { 

      System.Data.Common.DbDataRecord rd = (System.Data.Common.DbDataRecord)e.Item.DataItem; 

      Repeater nestedRepeater = e.Item.FindControl("NotesRepeater") as Repeater; 
      string FID = rd[0].ToString(); 


      using (cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con)) ; 

      SqlDataReader noteDrClient = cmd2.ExecuteReader(); //no data is being filled to the data reader... even though this command pulls data in SQL Server Management Studio. 

      if (noteDrClient.Read()) { //bind the repeater if there is data to bind 
        nestedRepeater.DataSource = noteDrClient; 
        nestedRepeater.DataBind(); 
      } 

      noteDrClient.Close();         




     } 
+0

您在Management Studio中的同一個用戶在連接字符串中配置的用戶登錄? – Jupaol

+0

是的,相同的用戶名。 –

+1

http://msdn.microsoft.com/en-us/library/h32h3abf%28v=vs.80%29.aspx –

回答

0

我通過創建一個額外的連接字符串,而不是重用我一直在使用的主要中繼器相同的連接字符串解決了這個問題。數據仍然沒有約束力,但它確實存在。

  using (cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con2)) ; 
0

我認爲查詢中的分號可能會導致問題。

嘗試使用像這樣在一旁值引號:

SELECT * FROM notes WHERE FID = '1356;'

如果分號是不是值的一部分:

SELECT * FROM notes WHERE FID = '1356'

+0

分號表示SQL語句的結尾。如果FID的數據類型是INT,則不需要引號,但分號是有效的。 –

2

您使用的語句是在處置SqlCommand在您有機會使用它之前。此外,您正試圖綁定到DataReader。將數據讀取器的結果獲取到「註釋」實體集合中,並將其綁定到集合。

 using (SqlCommand cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con)) 
     { 

      using(SqlDataReader noteDrClient = cmd2.ExecuteReader()) 
      { 

       while (noteDrClient.Read()) 
       { 
        Note n = new Note(); 
        ... get note from data reader 
        notes.Add(n); // add note to collection 
       } 
      } 
     } 

     // bind child 
     nestedRepeater.DataSource = notes;  
     nestedRepeater.DataBind();  

編輯:

你可能想看看到DataAdapter - http://www.mikesdotnetting.com/Article/57/Displaying-One-To-Many-Relationships-with-Nested-Repeaters