//建立一個(gè)表
DataTable tDataTable = new DataTable("test");
//建立一個(gè)列
DataColumn tDataCol = new DataColumn("name", System.Type.GetType("System.String"));
//添加列
tDataTable.Columns.Add(tDataCol);
tDataCol = new DataColumn("age", System.Type.GetType("System.Int32"));
//添加列
tDataTable.Columns.Add(tDataCol);
//添加每行數(shù)據(jù)
DataRow tDataRow = tDataTable.NewRow();
tDataRow["name"] = "xiaoming";
tDataRow["age"] = 14;
tDataTable.Rows.Add(tDataRow);
成都創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營銷策劃、網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、微信小程序開發(fā)、H5高端網(wǎng)站建設(shè)、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、全網(wǎng)營銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
DataRow tDataRowOne = tDataTable.NewRow();
tDataRowOne["name"] = "xiaoqiang";
tDataRowOne["age"] = 24;
tDataTable.Rows.Add(tDataRowOne);
//遍歷Datatable
foreach (DataRow t in tDataTable.Rows)
{
object Name = t[0];
object age = t[1];
}
//刪除數(shù)據(jù)
DataRow[] tDelet = tDataTable.Select("name = 'xiaoming'");
tDataTable.Rows.Remove(tDelet[0]);
public DataTable GetAllDataTable(DataSet ds)
{
DataTable newDataTable = ds.Tables[0].Clone(); //創(chuàng)建新表 克隆以有表的架構(gòu)。
object[] objArray = new object[newDataTable.Columns.Count]; //定義與表列數(shù)相同的對(duì)象數(shù)組 存放表的一行的值。
for (int i = 0; i < ds.Tables.Count; i++)
{
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
ds.Tables[i].Rows[j].ItemArray.CopyTo(objArray, 0); //將表的一行的值存放數(shù)組中。
newDataTable.Rows.Add(objArray); //將數(shù)組的值添加到新表中。
}
}
return newDataTable; //返回新表。
}
//------------------------------------------------------------------------------------------------------------------------------
將兩個(gè)DataTable合并成一個(gè)DataTable //兩個(gè)結(jié)構(gòu)一樣的DT合并
DataTable DataTable1 = new DataTable();
DataTable DataTable2 = new DataTable();
DataTable newDataTable = DataTable1.Clone();
object[] obj = new object[newDataTable.Columns.Count];
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
DataTable1.Rows[i].ItemArray.CopyTo(obj,0);
newDataTable.Rows.Add(obj);
}
for (int i = 0; i < DataTable2.Rows.Count; i++)
{
DataTable2.Rows[i].ItemArray.CopyTo(obj,0);
newDataTable.Rows.Add(obj);
}
//兩個(gè)結(jié)構(gòu)不同的DT合并
///
/// 將兩個(gè)列不同的DataTable合并成一個(gè)新的DataTable
///
///
private DataTable UniteDataTable(DataTable dt1, DataTable dt2, string DTName)
{
DataTable dt3 = dt1.Clone();
for (int i = 0; i < dt2.Columns.Count; i++)
{
dt3.Columns.Add(dt2.Columns[i].ColumnName);
}
object[] obj = new object[dt3.Columns.Count];
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt1.Rows[i].ItemArray.CopyTo(obj, 0);
dt3.Rows.Add(obj);
}
if (dt1.Rows.Count >= dt2.Rows.Count)
{
for (int i = 0; i < dt2.Rows.Count; i++)
{
for (int j = 0; j < dt2.Columns.Count; j++)
{
dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
}
}
}
else
{
DataRow dr3;
for (int i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)
{
dr3 = dt3.NewRow();
dt3.Rows.Add(dr3);
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
for (int j = 0; j < dt2.Columns.Count; j++)
{
dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
}
}
}
dt3.TableName = DTName; //設(shè)置DT的名字
return dt3;
}
標(biāo)題名稱:C#DataTable用法
鏈接分享:
http://weahome.cn/article/ggsejo.html