且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

新列和数据添加到已经包含数据的数据表 - C#

更新时间:2023-12-01 09:48:04

只要保持你的代码去 - 你在正确的轨道上:

  //调用SQL助手类来获得初始数据
DataTable的DT = sql.ExecuteDataTable(sp_MyProc);

dt.Columns.Add(NewColumn的typeof(System.Int32));

的foreach(DataRow的行中dt.Rows)
{
//需要设定值NewColumn柱
行[NewColumn] = 0; //或将其设置为其他值
}

//这里可能保存您的数据集,设置所有新的值


How do I add a new DataColumn to a DataTable object that already contains data?

PseudoCode

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", type(System.Int32));

foreach(DataRow row in dr.Rows)
{
    //need to set value to NewColumn column
}

Just keep going with your code - you're on the right track:

//call SQL helper class to get initial data 
DataTable dt = sql.ExecuteDataTable("sp_MyProc");

dt.Columns.Add("NewColumn", typeof(System.Int32));

foreach(DataRow row in dt.Rows)
{
    //need to set value to NewColumn column
    row["NewColumn"] = 0;   // or set it to some other value
}

// possibly save your Dataset here, after setting all the new values