2016-07-06 117 views
0

我有一個像下面如何使用

結果集的dataset數據集的欄目[![數據集] [1] [1]

現在我想添加IF條件檢查像低於

if(dataset rows(Usermail) == 10000){then go ahead} 

這是我的代碼。

DataSet ds = new DataSet(); 
     using (SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString())) 
     { 
      SqlCommand sqlComm = new SqlCommand("GET_INWARD_REMINDER_REPORT", conn); 
      sqlComm.CommandType = CommandType.StoredProcedure; 
      SqlDataAdapter da = new SqlDataAdapter(); 
      da.SelectCommand = sqlComm; 
      da.Fill(ds); 

      if(DataSet rowchecking) 
      { 

      } 
     } 

所以我的問題,如何檢查和比較數據集列。

+0

您需要訪問行中的字段,獲取其值,而不是列。 – Alex

+0

@Alex:只有我想知道,如何實現。 – BNN

回答

1

你可以像下面:

int First = Convert.ToInt32(ds.Tables[0].Rows[0]["columnName1"].ToString()); 
string Second = ds.Tables[0].Rows[0]["columnName2"].ToString(); 

因此,對於你的情況下,可以如:

foreach (DataRow dr in ds.Tables[0].Rows) 
{ 
    if(dr["UserEmail"].ToString() == "10000") 
    { 
     //do something; 
    }   
} 
+0

但它只有一個郵件,我們可以循環嗎? – BNN

+0

請檢查更新。 –

+0

這是工作。謝謝Rahul – BNN

1

可以使用foreach循環行和使用DataRow.Field獲得的電子郵件:

foreach(DataRow row in ds.Tables[0].Rows) 
{ 
    if(row.Field<string>("UserEmail") == "10000") 
     continue; // or revert it and do something if UserEmail != "1000" 
} 
+0

錯誤爲** system.data.dataset'在'Table'中不包含'tables'的定義** – BNN

+0

@NK:修正了它,它的'Tables'不是'Table' –

+0

是的,也在行'行.Field (「UserEmail」))'我得到錯誤,因爲**不能隱式地將字符串轉換爲bool – BNN

0

填充數據集之後。

if (ds.Tables[0].Rows.Count > 0) 
{ 
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
    { 
    if(ds.Tables[0].Rows[i]["UserEmail"].ToString() == "10000") 
    { 
    //do something; 
    } 
    } 
}