gpt4 book ai didi

c# - 将行添加到数据表 #2

转载 作者:行者123 更新时间:2023-11-30 12:33:25 26 4
gpt4 key购买 nike

我有一个数据表,有些列是字符串,有些是小数。当我添加一行时,它会自动转换信息还是我必须自己转换它们?我有很多数据需要添加到表格中,目前我正在使用它:

DataRow row = dataTable.NewRow();
row["item1"] = Info[0];
row["Item2"] = Info[1];
row["item3"] = Info[2];
row["Item4"] = Convert.ToDecimal(Info[3]);

最佳答案

row["..."] 是一个对象,可以采用任何类型。如果您的 Info[n] 是一个字符串,您可以根据需要将其转换为正确的类型。我不知道 Info 是否是一个集合,但如果是,为什么不做这样的事情:

List<Info> infoList = new List<Info>();
infoList.Add(...); //Add item here.

foreach(Info info in infoList)
{
DataRow row = dataTable.NewRow();
row["item1"] = info.Item1; //where Item1 could be a string
row["Item2"] = info.Item2; //where Item2 could be an int
row["item3"] = info.Item3; //Where Item3 could be a DateTime
row["Item4"] = info.Item4; //Where Item4 could be a Decimal
}

关于c# - 将行添加到数据表 #2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com