我有一个小应用程序,它连接到Oracle数据库,该数据库存储培训班的课程完成情况。数据库包含员工姓名、员工编号、课程完成日期和课程名称。我有返回一行的代码,如下所示:
String strPerIdNo = textBox1.Text;
String strQuery = "PER_ID_NO = " + "'" + strPerIdNo + "'";
DataRow[] foundRows;
foundRows = dataSet1.DataTable1.Select(strQuery);行包含一个由5个元素组成的数组:
>H 1104- String对象H 211f 212
我想从数组中的DateTime对象实例化一个DateTime对象,但是我不知道如何实现。我想显示一条消息,其中包含emp名称和他们完成课程的日期。
谢谢!
发布于 2011-07-11 23:53:24
好的,首先,这里没有实例化任何东西,因为您想要的DateTime已经存在(否则,它不会在DataRow中!)。此外,DateTime是值类型,而不是引用类型;考虑实例化值类型是不正常的;您只需初始化或复制它们。
您要寻找的是获取DateTime元素在您的DataRow中的值。以下是几个步骤:
//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;https://stackoverflow.com/questions/6657993
复制相似问题