DataColumn DataType int and long
本文关键字:long and int DataType DataColumn | 更新日期: 2023-09-27 18:23:41
是否可以在包含int和long值的DataTable中有一个DataColumn?如果是有效的int,它应该返回一个int值,否则它应该返回长值。
出于某种背景原因,我想要实现这一点,我发布了一个创建int类型DataColumn的产品,但它从一开始就应该很长时间。现在,我不想破坏将值显式转换为int的现有项目,但我希望在列中允许长值。
我可以在DataTable上订阅一个事件来将值转换为int吗?
以下代码应正确执行。
DataTable dataTable = new DataTable();
dataTable.Columns.Add("IntAndLong", typeof(long));
DataRow intRow = dataTable.NewRow();
intRow[0] = 123;
dataTable.Rows.Add(intRow);
DataRow longRow = dataTable.NewRow();
longRow[0] = 3000000000;
dataTable.Rows.Add(longRow);
long longValue = (long)dataTable.Rows[1][0];
int intValue = (int)dataTable.Rows[0][0];
我建议收集一个长类型变量中的datacolumn值,检查其值是否小于int.MaxValue并大于int.MinValue,然后如果需要,将其强制转换为int变量。