如何在 .NET 1.1 中分配数据行对象

发布于 2024-08-25 17:56:24 字数 241 浏览 2 评论 0原文

目前我正在这样做...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时间你老了 2024-09-01 17:56:24

您当前正在做什么...

object s = new object();
s = mydatarow["mycolumn"];

这不是一个好主意,因为第一行创建一个新对象,然后第二行丢弃该对象并将其替换为 DataRow 中“mycolumn”列中的值。这实际上并没有多大伤害,只是它无缘无故地为垃圾收集器创造了额外的工作。相反,您应该这样做:

object s = mydatarow["mycolumn"];

但是,如果您碰巧知道“mycolumn”包含字符串值,则可以这样做:

string s = (string)mydatarow["mycolumn"];

同样,如果“mycolumn”是整数列...

int x = (int)mydatarow["mycolumn"];

在任何情况下都不应该这样做下面的内容,因为它根本没有任何意义,并且框架足够好地告诉您 DataRow 中的“mycolumn”值实际上不是另一个 DataRow。

DataRow r = null;
r = mydatarow["mycolumn"];

What you're currently doing...

object s = new object();
s = mydatarow["mycolumn"];

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:

object s = mydatarow["mycolumn"];

However, if you happen to know that "mycolumn" contains a string value, you can instead do this:

string s = (string)mydatarow["mycolumn"];

Likewise, if "mycolumn" is an integer column...

int x = (int)mydatarow["mycolumn"];

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.

DataRow r = null;
r = mydatarow["mycolumn"];
笨笨の傻瓜 2024-09-01 17:56:24

看起来你做错了什么。

DataRow r = null;
r = mydatarow["mycolumn"];

mydatarow 是 DataRow 本身。当您引用特定列时,您会获得表格的“单元格”。 (它具有类型对象,因为数据集基本上是松散类型的)但是您尝试将单元格分配给数据行。

知道了?

looks like you do something wrong.

DataRow r = null;
r = mydatarow["mycolumn"];

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?

浅唱ヾ落雨殇 2024-09-01 17:56:24

共有三部分...数据表(多行)、表的数据行(多个字段/列)以及各个列单元格本身。

DataTable oTbl = ResultSetFromYourQuery();
DataRow oDR = oTbl.Rows[0];  // Rows[0] is first row in a result table (zero-based)
String ColumnValue = oDR["YourColumnName"];

There are three things... a DataTable (multiple rows), a data ROW (multiple fields/columns) of a TABLE, and the individual column cells themselves.

DataTable oTbl = ResultSetFromYourQuery();
DataRow oDR = oTbl.Rows[0];  // Rows[0] is first row in a result table (zero-based)
String ColumnValue = oDR["YourColumnName"];
绾颜 2024-09-01 17:56:24

您可以尝试一下:

DataRow r = null;
r = (DataRow) mydatarow["mycolumn"]; //*

这不太可能起作用,但理论上是可能的。
通常,当我遇到这些问题时,我会坚持断点(我添加了 //*),观看 mydatarow["mycolumn"],然后Visual Studio 告诉我它实际上是什么类型(以及其他需要确定的事情)。

You can try this:

DataRow r = null;
r = (DataRow) mydatarow["mycolumn"]; //*

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 //*), watch mydatarow["mycolumn"], and have Visual Studio tell me what type it actually is (among other things to determine).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文