1
DataTable dt = new DataTable("TableName"); // 创建表
dt.Columns.Add("ColName1", System.Type.GetType("System.String")); // 添列
dt.Columns.Add("ColName2", System.Type.GetType("System.Int32"));
...
DataRow dr = dt.NewRow(); // 创建一行
dr["ColName1"] = this.requestNoHidden.Value; // 行赋值
if (!string.IsNullOrEmpty(this.txtAmount1.Text))
{
dr["ColName2"] = this.txtAmount1.Text;
}
else
{
dr["ColName2"] = DBNull.Value;
}
...
dt.Rows.Add(dr); // 添行 到Table
ds.Tables.Add(dt); // 添Table 到DataSet
说明:Int32型列,不能将空(NUll、“”)赋值于行,要么不赋值,要么赋为DBNull.Value.
--------------------------------------------------------------------------------------------------------
2
// 明细表获取
DataRow[] drsMeisai = drKey.GetChildRows(ds.Relations[0]);
// 创建明细表
DataTable dtBind = ds.Tables["TableName"].Clone(); // 获取已知表
foreach (DataRow dr in drsMeisai)
{
dtBind.ImportRow(dr);
}
说明:用于组合已知表结构(列已封装好),插入已知行的快捷方式。
转载于:https://www.cnblogs.com/xinxinbb/archive/2008/08/13/1266813.html