隨時更新
C# 常用處理DATATABLE的用法
常用的table處理方法
//挑選加入新的table內
資料表 = _Table.Select("SEL","").CopyToDataTable();
//加入新的table內
foreach (DataRow oRow in PopForm.資料表.Rows)
{
object Obj = Ds.Tables["Detail"].Compute("MAX(INVSEQ)", "");
DataRow NewRow = Ds.Tables["Detail"].NewRow();
NewRow["INVSEQ"] = Convert.IsDBNull(Obj) ? "001" : (Convert.ToInt16(Obj) + 1).ToString().PadLeft(3, '0');
NewRow["INVPNO"] = oRow["BOMCO1"].ToString();
NewRow["CNAME"] = oRow["INCDS"].ToString();
NewRow["INSIZ"] = oRow["INSIZ"].ToString();
NewRow["INVREM"] = oRow["BOMCOD"].ToString();
NewRow["TOOLNO"] = oRow["BOMTYP1"].ToString();
NewRow["KNVDESC"] = "刀具磨損";
Ds.Tables["Detail"].Rows.Add(NewRow);
}
//判斷一個字符串是否為DataTable的列名
dtInfo.Columns.Contains("AX");
//SORT
dt.DefaultView.Sort = "ID ,Name ASC";
dt=dt.DefaultView.ToTable();
//DataTable convert to XML and XML convert to DataTable
public string ConvertBetweenDataTableAndXML_AX(DataTable dtNeedCoveret)
{
System.IO.TextWriter tw = new System.IO.StringWriter();
//if TableName is empty, WriteXml() will throw Exception.
dtNeedCoveret.TableName=dtNeedCoveret.TableName.Length==0?"Table_AX":dtNeedCoveret.TableName;
dtNeedCoveret.WriteXml(tw);
dtNeedCoveret.WriteXmlSchema(tw);
return tw.ToString();
}
public DataTable ConvertBetweenDataTableAndXML_AX(string xml)
{
System.IO.TextReader trDataTable = new System.IO.StringReader(xml.Substring(0, xml.IndexOf("<?xml")));
System.IO.TextReader trSchema = new System.IO.StringReader(xml.Substring(xml.IndexOf("<?xml")));
DataTable dtReturn = new DataTable();
dtReturn.ReadXmlSchema(trSchema);
dtReturn.ReadXml(trDataTable);
return dtReturn;
}
實做用法
DataTable dt_AX = new DataTable();
//dt_AX.Columns.Add("Sex", typeof(System.Boolean));
//DataRow dr = dt_AX.NewRow();
//dr["Sex"] = true;
//dt_AX.Rows.Add(dr);
string xml=ConvertBetweenDataTableAndXML_AX(dt_AX);
DataTable dt = ConvertBetweenDataTableAndXML_AX(xml);
興滿逸筑2013年全新完工,位於恆春鎮外圍離市區只要2分鐘車程,有豪華的外觀、有美麗的庭院、也有溫馨舒適的房間,這裡車流量不多,空氣不錯,適合家族或成群結黨的朋友們一同旅遊的好地方。