如何在 .NET 1.1 中分配数据行对象
目前我正在这样做...
object s = new object();
s = mydatarow["mycolumn"];
我想做的是...
DataRow r = null;
r = mydatarow["mycolumn"];
并且我收到一条错误消息,指出无法从对象转换为 DataRow。
有更好的方法吗?
currently I am doing this...
object s = new object();
s = mydatarow["mycolumn"];
What I would like to do is...
DataRow r = null;
r = mydatarow["mycolumn"];
and I get an error saying that can not covert from object to DataRow.
is there a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您当前正在做什么...
这不是一个好主意,因为第一行创建一个新对象,然后第二行丢弃该对象并将其替换为 DataRow 中“mycolumn”列中的值。这实际上并没有多大伤害,只是它无缘无故地为垃圾收集器创造了额外的工作。相反,您应该这样做:
但是,如果您碰巧知道“mycolumn”包含字符串值,则可以这样做:
同样,如果“mycolumn”是整数列...
在任何情况下都不应该这样做下面的内容,因为它根本没有任何意义,并且框架足够好地告诉您 DataRow 中的“mycolumn”值实际上不是另一个 DataRow。
What you're currently doing...
This isn't a good idea, because the first line creates a new object, and then the second line throws away that object and replaces it with the value from the "mycolumn" column in the DataRow. This doesn't really hurt much, except that it creates extra work for the garbage collector for no good reason. Instead of that, you should do this:
However, if you happen to know that "mycolumn" contains a string value, you can instead do this:
Likewise, if "mycolumn" is an integer column...
Under no circumstances should you do the following, because it simply doesn't make any sense, and the framework is nice enough to tell you that the "mycolumn" value in your DataRow is not, in fact, another DataRow.
看起来你做错了什么。
mydatarow 是 DataRow 本身。当您引用特定列时,您会获得表格的“单元格”。 (它具有类型对象,因为数据集基本上是松散类型的)但是您尝试将单元格分配给数据行。
知道了?
looks like you do something wrong.
mydatarow is DataRow itself. when you reference to specific column you get "cell" of the table. (it has type object, because DataSets basically are loosely typed) but you try to assign cell to DataRow.
got it?
共有三部分...数据表(多行)、表的数据行(多个字段/列)以及各个列单元格本身。
There are three things... a DataTable (multiple rows), a data ROW (multiple fields/columns) of a TABLE, and the individual column cells themselves.
您可以尝试一下:
这不太可能起作用,但理论上是可能的。
通常,当我遇到这些问题时,我会坚持断点(我添加了
//*
),观看mydatarow["mycolumn"]
,然后Visual Studio 告诉我它实际上是什么类型(以及其他需要确定的事情)。You can try this:
It is unlikely, but theoretically possible, that this will work.
Usually when I'm having trouble with this stuff I'll stick in a breakpoint (where I added
//*
), watchmydatarow["mycolumn"]
, and have Visual Studio tell me what type it actually is (among other things to determine).