2017-01-26 198 views
0
public queue getQueueDataByID(int queueID) 
    { 


     string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 

     SqlDataAdapter da; 
     DataSet ds = new DataSet(); 
     queue myQueue = new queue(); 

     StringBuilder sqlCommand = new StringBuilder(); 
     sqlCommand.AppendLine("Select * from QueueTable where"); 
     sqlCommand.AppendLine("id = @paraId"); 

     try 
     { 
      SqlConnection myConn = new SqlConnection(DBConnect); 

      // sqlCommand.Parameters.AddWithValue("paraCustId", tbCustId.Text); 
      da = new SqlDataAdapter(sqlCommand.ToString(), myConn); 
      da.SelectCommand.Parameters.AddWithValue("paraId", queueID); 
      // fill dataset 
      da.Fill(ds, "QueueTable"); 
      //conn.Close(); 
      int rec_cnt = ds.Tables["QueueTable"].Rows.Count; 
      DataRow row = ds.Tables["QueueTable"].Rows[rec_cnt-1 ]; 
      myQueue.queueNo = row["QueueNo"].ToString(); 
      myQueue.NRIC = row["NRIC"].ToString(); 
      myQueue.ContactNo = row["ContactNo"].ToString(); 

       } 

     catch (SqlException ex) 
     { 

     } 
     return myQueue; 
    } 

這是我的課最後一行中獲得價值,我試圖從數據庫中檢索在Web表單中顯示的最後一排的DATAS,任何想法我怎麼能做到這一點?我的「id」,主鍵被設置爲自動增量。從SQL Server數據庫C#

+0

這段代碼拋出異常或不返回最後一排? – jjj

+0

嗨,謝謝你的回覆,主要觀點是,我不知道如何檢索,或者更喜歡,我不知道在我的web表單中使用什麼代碼來檢索。 – ShengisBored

+0

因爲你需要綁定這個隊列到web表單fields.Please參考http://stackoverflow.com/questions/18800566/how-to-bind-data-in-html-table-in-asp-net-web -form – jjj

回答

0

您可以使用數據表上的Linq來獲取最後一行。

var queueDataTable = ds.Tables[0]; 
var lastDataRow = queueDataTable.AsEnumerable().Last(); 

如果需要通過所有行的數據表

foreach (var dataRow in queueDataTable.AsEnumerable()) 
{ 

} 

感謝中itterate

+0

嗨,謝謝你的回覆 這聽起來很愚蠢,但我應該把代碼 – ShengisBored

+0

沒問題。如果尚未使用返回數據集或數據表的公共方法,則需要將數據庫訪問代碼封裝在數據層類中。這應該被引用並由服務/笨拙層調用,該層根據返回的數據處理最後一行並填充Queue對象,最終將其返回給調用Web方法。您將進入設計領域,儘管這裏不討論這個問題。 – Wheels73

+0

太棒了。謝謝! – ShengisBored