首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从DateTime对象中提取数据,该对象在DataRow中?

从DateTime对象中提取数据,该对象在DataRow中?
EN

Stack Overflow用户
提问于 2011-07-11 23:39:44
回答 1查看 1.5K关注 0票数 1

我有一个小应用程序,它连接到Oracle数据库,该数据库存储培训班的课程完成情况。数据库包含员工姓名、员工编号、课程完成日期和课程名称。我有返回一行的代码,如下所示:

代码语言:javascript
复制
            String strPerIdNo = textBox1.Text;
            String strQuery = "PER_ID_NO = " + "'" + strPerIdNo + "'";
            DataRow[] foundRows;
            foundRows = dataSet1.DataTable1.Select(strQuery);

行包含一个由5个元素组成的数组:

  • - System.DateTime object
  • 1 - String object
  • 2 - Int object
  • 3 - String object

>H 1104- String对象H 211f 212

我想从数组中的DateTime对象实例化一个DateTime对象,但是我不知道如何实现。我想显示一条消息,其中包含emp名称和他们完成课程的日期。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-11 23:53:24

好的,首先,这里没有实例化任何东西,因为您想要的DateTime已经存在(否则,它不会在DataRow中!)。此外,DateTime是值类型,而不是引用类型;考虑实例化值类型是不正常的;您只需初始化或复制它们。

您要寻找的是获取DateTime元素在您的DataRow中的值。以下是几个步骤:

代码语言:javascript
复制
//step 0: make sure there's at least one DataRow in your results
if (foundRows.Length < 1) { /* handle it */ }

//step 1: get the first DataRow in your results, since you're sure there's one
DataRow row = foundRows[0];

//step 2: get the DateTime out of there
object possibleDate = row["TheNameOfTheDateColumn"];

//step 3: deal with what happens if it's null in the database
if (possibleDate == DBNull.Value) { /* handle it */ }

//step 4: possibleDate isn't a DateTime - let's turn it into one.
DateTime date = (DateTime)possibleDate;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6657993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档