DataTable foreach 怎麼操作

每次都忘了foreach datatable要怎麼做,先給它記下來。

語法是這樣寫的「foreach(DataRow row in mydatatable.Rows){….}」。

    
    query_id = Request.QueryString["query_id"];

    vacation_month = Request.QueryString["vacation_month"];

    query_string = string.Format(query_string, query_id, vacation_month);

    mydataadapter = getSqlDataAdapter(query_string);

    mydataadapter.Fill(mydatatable);//資料充入DataTable
    if (mydatatable.Rows.Count == 0) { Response.Close(); }//查無資料關閉連線

    sb.Append("<?xml version=\"1.0\"?>");

    foreach(DataRow row in mydatatable.Rows){
        string xml_temp_string="";
        xml_temp_string = @"<datarow><employee_id>{0}</employee_id><vacation_num>{1}</vacation_num>
<vacation_subtotal>{2}</vacation_subtotal><unit>{3}</unit><belong_month>{4}</belong_month></datarow>";
        xml_temp_string = string.Format(xml_temp_string, row["員工編號"], row["假別代號"], row["數量統計"], row["單位"], row["歸屬月份"]);
        sb.Append(xml_temp_string);
    }

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s