提问人:J.Doe 提问时间:8/19/2022 更新时间:8/19/2022 访问量:618
DataRow.ItemArray 没有值,并且为空
DataRow.ItemArray does not have values and is empty
问:
无论我如何尝试初始化它,我的 DataRow 对象都是 emtpy。源代码如下:
private DataTable ReadFileIntoDb(MemoryStream file)
{
file.Position = 0;
var sr = new StreamReader(file,Encoding.GetEncoding(1251),true);
var dt = new DataTable();
Console.WriteLine("datatable success");
dt.Columns.Add(new DataColumn("description", typeof(String)));
dt.Columns.Add(new DataColumn("code", typeof(String)));
dt.Columns.Add(new DataColumn("cardnumber", typeof(String)));
dt.Columns.Add(new DataColumn("costcentrecode", typeof(int)));
dt.Columns.Add(new DataColumn("costcentre", typeof(String)));
dt.Columns.Add(new DataColumn("article_description", typeof(String)));
dt.Columns.Add(new DataColumn("trans_date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("article_price", typeof(Double)));
dt.Columns.Add(new DataColumn("article_quantity", typeof(Double)));
dt.Columns.Add(new DataColumn("netto", typeof(Double)));
dt.Columns.Add(new DataColumn("brutto", typeof(Double)));
dt.Columns.Add(new DataColumn("mwst", typeof(Double)));
dt.Columns.Add(new DataColumn("beleg", typeof(Double)));
dt.Columns.Add(new DataColumn("oberverkaufsgruppe", typeof(int)));
dt.Columns.Add(new DataColumn("house", typeof(String)));
dt.Columns.Add(new DataColumn("id", typeof(String)));
//1. row gets skipped
string line = sr.ReadLine();
Console.WriteLine("line contains:" + line);
line = sr.ReadLine();
do
{
string[] itemArray = line.Split(';');
DataRow row = dt.NewRow();
row.ItemArray = itemArray; //doesnt work and throws ArgumentException
itemArray.CopyTo(row.ItemArray, 0); //doesnt work either
for (int i = 0; i < 14; i++) //also doesnt work and leaves it empty
{
row.ItemArray[i] = itemArray[i];
Console.WriteLine(row.ItemArray[i]);
}
row["id"] = Guid.NewGuid().ToString();
row["house"] = "";
dt.Rows.Add(row);
line = sr.ReadLine();
} while (!string.IsNullOrEmpty(line));
ReadFileIntoDb 获取 csv 文件的内容作为参数。现在,我将csv内容减少到只有两行,其中一行只是标题。内容如下所示,并成功解析为 var sr: Bewohner;1102284;1102284;42600;ASG腐烂;Menü (Inklusivleistung);01.07.2022;0;1;;;;;2000003
如您所见,此文件包含 14 个元素,其中一些是空的。我的 DataTable 还包含 14 列和 2 个附加列,但是我在代码中设置了它们,它们的值不是来自 csv。 执行数据库操作后,我的表仍然为空,因为我相信那一行。ItemArray 始终保持为空,因此不会向数据库添加任何内容。但我不明白为什么它保持为空以及为什么我得到一个 ArgumentException。源数组 itemArray 不为空,长度也为 14。那么为什么会抛出异常 if row.ItemArray 的长度更大?它有 16 个。
答:
1赞
Sund'er
8/19/2022
#1
看看这个,我稍微改变了输入字符串
我添加的行是
for (int i = 0; i < itemArray.Length; i++)
{
if (string.IsNullOrWhiteSpace(itemArray[i]))
{
itemArray[i] = null;
}
}
为了在给定的输入中将空值替换为 null,没有这个我得到了错误
Unhandled exception. System.ArgumentException: Input string was not in a correct format.Couldn't store <> in netto Column. Expected type is Double.
---> System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
at System.Data.Common.DoubleStorage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)
--- End of inner exception stack trace ---
at System.Data.DataColumn.set_Item(Int32 record, Object value)
at System.Data.DataRow.set_ItemArray(Object[] value)
at Program.Main()
Command terminated by signal 6
using System;
using System.Data;
public class Program
{
public static void Main()
{
string sp="Bewohner;1102284;1102284;42600;ASG ROT;Menü (Inklusivleistung);01.07.2022;0;1;;;;;2000003^Bewohner2;11022842;11022824;426200;ASG2 ROT;Menü (Inklusivleistung);01.07.2022;0;1;;;;;2000004";
var dt = new DataTable();
Console.WriteLine("datatable success");
dt.Columns.Add(new DataColumn("description", typeof(String)));
dt.Columns.Add(new DataColumn("code", typeof(String)));
dt.Columns.Add(new DataColumn("cardnumber", typeof(String)));
dt.Columns.Add(new DataColumn("costcentrecode", typeof(int)));
dt.Columns.Add(new DataColumn("costcentre", typeof(String)));
dt.Columns.Add(new DataColumn("article_description", typeof(String)));
dt.Columns.Add(new DataColumn("trans_date", typeof(DateTime)));
dt.Columns.Add(new DataColumn("article_price", typeof(Double)));
dt.Columns.Add(new DataColumn("article_quantity", typeof(Double)));
dt.Columns.Add(new DataColumn("netto", typeof(Double)));
dt.Columns.Add(new DataColumn("brutto", typeof(Double)));
dt.Columns.Add(new DataColumn("mwst", typeof(Double)));
dt.Columns.Add(new DataColumn("beleg", typeof(Double)));
dt.Columns.Add(new DataColumn("oberverkaufsgruppe", typeof(int)));
dt.Columns.Add(new DataColumn("house", typeof(String)));
dt.Columns.Add(new DataColumn("id", typeof(String)));
string[] mainarray = sp.Split('^');
for(int a=0;a<mainarray.Length;a++)
{
string[] itemArray = mainarray[a].Split(';');
DataRow row = dt.NewRow();
for (int i = 0; i < itemArray.Length; i++)
{
if (string.IsNullOrWhiteSpace(itemArray[i]))
{
itemArray[i] = null;
}
}
row.ItemArray = itemArray; //doesnt work and throws ArgumentException
itemArray.CopyTo(row.ItemArray, 0); //doesnt work either
for (int i = 0; i < 14; i++) //also doesnt work and leaves it empty
{
row.ItemArray[i] = itemArray[i];
//Console.WriteLine(row.ItemArray[i]);
}
row["id"] = Guid.NewGuid().ToString();
row["house"] = "";
dt.Rows.Add(row);
}
foreach(DataRow dataRow in dt.Rows)
{var ix = 0;
foreach(var item in dataRow.ItemArray)
{
Console.WriteLine(ix+"-----"+item);
ix++;
}
}
}
}
输出:
datatable success
0-----Bewohner
1-----1102284
2-----1102284
3-----42600
4-----ASG ROT
5-----Menü (Inklusivleistung)
6-----01/07/2022 00:00:00
7-----0
8-----1
9-----
10-----
11-----
12-----
13-----2000003
14-----
15-----ccf0a446-610e-4396-896d-7e8c4a43f729
0-----Bewohner2
1-----11022842
2-----11022824
3-----426200
4-----ASG2 ROT
5-----Menü (Inklusivleistung)
6-----01/07/2022 00:00:00
7-----0
8-----1
9-----
10-----
11-----
12-----
13-----2000004
14-----
15-----0c9cb9b1-8640-4626-8790-0d80fad9d8da
评论
0赞
J.Doe
8/19/2022
我还有一个问题是,为什么你得到正确的日期 01/07/2022,而在我的控制台输出中,我把日期和月份交换到 07.01.2022 ??
0赞
Sund'er
8/20/2022
我在IST时区运行,你运行的是哪个时区?
评论