更改每个数据行上的列数据
本文关键字:数据 | 更新日期: 2023-09-27 17:55:55
我正在尝试访问DataSet
以更改DataSet
中每一行的列值。
我从数据库中提取了一个DataSet dsCustomers
。从那DataSet
我选择了DataSet
中唯一的表。DataTable
表中有一列名为 BillingDate
。
我正在尝试将该列中的DateTime
变量添加 10 年。我正在尝试为数据表中的每一行执行此操作。老实说,我不知道该怎么做,也不知道我做错了什么。
下面的代码是我掌握如何做到这一点的最佳尝试。任何帮助都会被慷慨地接受。
SqlConnection sqlConnCustomer = new SqlConnection(cString);
DataSet dsCustomer = new DataSet();
try
{
dsCustomer = LearningData.SqlHelper.ExecuteDataset(sqlConnCustomer, "dbo.rptRecurringBilling_S");
DataTable table = dsCustomer.Tables[0];
int i = 0;
foreach (DataRow row in table.Rows)
{
row.AcceptChanges();
row.BeginEdit();
System.DateTime BillingDate = Convert.ToDateTime(table.Rows[i]["BillingDate"]);
BillingDate = BillingDate.AddYears(10);
row.EndEdit();
row.AcceptChanges();
i++;
}
}
试试这个
foreach (DataRow row in ds.Tables[0].Rows)
{
var ik = row["BillingDate"];
System.DateTime BillingDate = Convert.ToDateTime(ik);
BillingDate = BillingDate.AddYears(10);
row["VALUE"] = BillingDate;
}