在没有转换错误的情况下对 DataRow 中的非类型化字段进行拆箱

发布于 2024-09-04 11:30:32 字数 659 浏览 2 评论 0原文

我的 ASP.Net 应用程序的数据层调用存储过程来获取有关访问者登录时的少量(一条记录)信息。我传入他们的电话号码和存储过程,使用一个简单的 SELECT,传回 5 个字段,其中第一个是主键,一个 BIGINT。我的数据层获取 DataRow 并尝试用它创建一个数据对象。在数据对象中,表示主键的属性是 Int64。看起来像这样:

 sub = new PersistentSubscriber((String) dr["UserFirstName"],
        (String) dr["UserLastName"],
        phoneNumber, (Int64) dr["UserId"], (Byte) dr["SubscriberStatus"]);

我发现,当我有一个大的主键值(例如 88698)时,一切都运行良好。但是,当我得到一个小的主键值(例如 999)时,我会收到“指定的转换无效”错误当尝试设置该主键属性时。当我尝试在“立即”窗口中使用它时,我得到以下信息:

?(Int64)dr["UserId"]
Cannot unbox 'dr["UserId"]' as a 'long'
?(int)dr["UserId"]
999
?(Int32)dr["UserId"]
999

如果不求助于类型化数据集,我在这里做错了什么?

The data layer of my ASP.Net app calls a stored proc to get a small (one record) amount of information about a visitor upon login. I pass in their phone number and the sproc, using a simple SELECT, passes back 5 fields, the first of which is the primary key, a BIGINT. My data layer gets the DataRow and tries to create a data object with it. In the data object, the property that represents the primary key is an Int64. That looks like this:

 sub = new PersistentSubscriber((String) dr["UserFirstName"],
        (String) dr["UserLastName"],
        phoneNumber, (Int64) dr["UserId"], (Byte) dr["SubscriberStatus"]);

What I'm finding is that everything works great when I have a big primary key value, such as 88698. However, when I get a small one, like 999, I get a "Specified Cast is Invalid" error when trying to set up that primary key property. When I try playing with it in the Immediate window, I get this:

?(Int64)dr["UserId"]
Cannot unbox 'dr["UserId"]' as a 'long'
?(int)dr["UserId"]
999
?(Int32)dr["UserId"]
999

Without resorting to a typed dataset, what am I doing wrong, here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

寒江雪… 2024-09-11 11:30:32

当然,我正在挖掘一个老问题,但它出现在我的谷歌搜索顶部附近,所以为什么不......

你听说过 DataSetExtensions?它们允许您以强类型的方式访问 DataRow 中的字段。这在 linq 查询中非常有用,根据我的经验,这比调用 DataTable.Select() 快得多。

只需将对 System.Data.DataSetExtensions 的引用添加到您的项目中,您就可以使用它们了。

在示例中:

Int64 value = dr.Field<Int64>("UserId");

dr.SetField("UserId", value);

Granted I'm digging up an old question, but it showed up for me in a google search near the top so why not...

Have you heard of DataSetExtensions? They let you access fields in a DataRow in a strongly-typed way. This is extremely useful in linq queries, and in my experience can be significantly faster than calling DataTable.Select().

Just add a reference to System.Data.DataSetExtensions to your project, and you're ready use them.

In Example:

Int64 value = dr.Field<Int64>("UserId");

dr.SetField("UserId", value);
一梦等七年七年为一梦 2024-09-11 11:30:32

尝试一下Int64.TryParse

long userId;
if(Int64.TryParse(dr["UserId"], out userId))
{
    // successful conversion
}

Try giving Int64.TryParse a shot.

long userId;
if(Int64.TryParse(dr["UserId"], out userId))
{
    // successful conversion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文