2013-08-19 39 views
-3

我有一個返回布爾值的存儲過程。 (0或1)。它返回多行。我的問題是如何迭代所有的結果。獲取查詢結果中的所有行

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBReader"].ConnectionString)) 
    { 
     using (SqlCommand com = new SqlCommand("Reader.usp_CheckerIsStopped", con)) 
     { 
      com.CommandType = CommandType.StoredProcedure; 
      com.Parameters.Add("@fld_UserID", SqlDbType.Int).Value = this.UserID; 

      con.Open(); 
      SqlDataReader dr = com.ExecuteReader(); 

      if (dr.Read() == 1) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 

它有一個錯誤dr.Read() == 1

錯誤:

Operator == cannot be applied to type bool to int"

我的存儲過程返回一個包含0或1多行,我想是因爲我想要做的就是這些值檢查它是否等於假(0或1)

if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //if (e.Row.Cells[11].Text == "In Progress") 
      //{ 
      // System.Web.UI.WebControls.ImageButton StartImageButton = (System.Web.UI.WebControls.ImageButton)e.Row.Cells[1].FindControl("StartImageButton"); 
      // StartImageButton.Visible = false; 
      //} 
      gvfunct.UserID = Convert.ToInt32(Session["UserID"]); 
      gvfunct.CheckIsStopped(); 
      if (gvfunct.CheckIsStopped() == true) 
      { 
       System.Web.UI.WebControls.ImageButton StartImageButton = (System.Web.UI.WebControls.ImageButton)e.Row.Cells[2].FindControl("StartImageButton"); 
       StartImageButton.Visible = true; 
       System.Web.UI.WebControls.ImageButton StopImageButton = (System.Web.UI.WebControls.ImageButton)e.Row.Cells[1].FindControl("StopImageButton"); 
       StopImageButton.Visible = false; 
      } 
      else 
      { 
       System.Web.UI.WebControls.ImageButton StopImageButton = (System.Web.UI.WebControls.ImageButton)e.Row.Cells[1].FindControl("StopImageButton"); 
       StopImageButton.Visible = true; 
       System.Web.UI.WebControls.ImageButton StartImageButton = (System.Web.UI.WebControls.ImageButton)e.Row.Cells[2].FindControl("StartImageButton"); 
       StartImageButton.Visible = false; 
      } 

     } 
+0

什麼是結果集的樣子喜歡? 'Read'只會使每行中的指針前進。 –

+0

返回一個布爾值或返回多行? –

+0

你的問題很混亂。你想返回所有行,還是隻返回一個結果?如果它是一個結果,那麼結果代表什麼? –

回答

0

由於dr.Read()回報bool因此,你與int

比較,如果SqlDataReader的有行,否則爲false,它將返回true時收到錯誤。

因此改變你的代碼,

return dr.Read(); 

,而不是

if (dr.Read() == 1) 
{ 
    return true; 
} 
else 
{ 
    return false; 
} 
+0

我將在哪裏插入該行代碼? – user1954418

+0

'dr.Read()'只有在'dr'有另一行才能讀取時返回true。 'dr.Read()'讀取該行並讀取數據讀取器的下一行。 – DFord

0

更換

if (dr.Read() == 1) 
{ 
    return true; 
} 

if (dr.Read()) 
{ 
    return true; 
} 
1

你需要繼續Read()並對這些結果進行處理。

while (dr.Read()) 
{ 

} 

你看,Read()方法返回一個bool。所以,現在,如果你想獲得每行的結果,你可能會做這樣的事情:

while (dr.Read()) 
{ 
    var val = dr.GetInt32(0); 
} 

,並且將獲得的第一列的值,從該行你在Read()是目前,並將其投射爲int。當然,如果你試圖施放string或其他東西,那麼這條線可能會引發錯誤。考慮一個事實,即DataReader是一個只讀,只進的數據緩衝區。它實際上一次只能從服務器中拉出一行數據,因此在操作期間將連接打開,直到它超出範圍。

+0

如果我這樣做,我將如何返回真或假?雖然沒有其他條件 – user1954418

+0

@ user1954418,您將不得不設置一個「bool結果」變量並將其返回。即使在你的例子中,你所擁有的邏輯也不能在許多記錄中返回一個「true」或「false」。你需要充實這些邏輯,但是如果你需要遍歷許多記錄,你需要利用'while'因爲'Read()'返回一個'true'或者'false'(即繼續讀或者不讀,因爲我有/還沒有達到結束)。 –

-1

如果需要通過所有行迭代,試試這個

if(dr.Read()){ 
if(dr.Read()) return true; 
else return false; 
} 
else return false; 

這會讀博士兩次,如果發現2行返回true。如果發現0或1行,則爲假。

0

你需要顯式轉換或只是使用它的正確的布爾類型 所以不是if (dr.Read() == 1),你可以使用if (dr.Read() == true)if (dr.Read())

沒有一個直接鑄造的,我知道的,比如(bool)1是肯定不行,但是你可以一直使用Convert.ToBoolean(1)或其他一些方法,將它

你還可以創建自己的自定義鑄造方法

IntToBool (int bool) 
{ 
    if(bool == 1) return true; 
    return false; 
} 
0

這不是真的清楚你的存儲過程返回,但如果第一行和第一列例如包含一個整數,你也忘了閱讀器和使用SqlCommands ExecuteScalar-method像這樣:

return com.ExecuteScalar() == 1;