c#.net中dataset.table["数据表名称"].rows.count无法赋值给int型变量

2024-11-23 13:35:47
推荐回答(1个)
回答1:

语法没问题,你查查代码,跟一下断点,是不是其他某个地方把r和r2的值改变了。我写了一个例子,输出结果是:
r: 16
r2: 1

代码如下:
//给dataset1加16条数据
DataSet dataset1 = new DataSet();
DataTable exam1 = new DataTable("exam");
exam1.Columns.Add("Id");
for (int i = 0; i < 16; i++)
{
DataRow drItem = exam1.NewRow();
drItem["Id"] = i;
exam1.Rows.Add(drItem);
}
dataset1.Tables.Add(exam1);

//给dataset2加1条数据
DataTable exam2 = new DataTable("exam");
exam2.Columns.Add("Id");
for (int i = 0; i < 1; i++)
{
DataRow drItem = exam2.NewRow();
drItem["Id"] = i;
exam2.Rows.Add(drItem);
}
DataSet dataset2 = new DataSet();
dataset2.Tables.Add(exam2);

//赋值
int r = dataset1.Tables["exam"].Rows.Count;
int r2 = dataset2.Tables["exam"].Rows.Count;

//输出
Console.WriteLine("r:"+r.ToString());
Console.WriteLine("r2:" + r2.ToString());