简体   繁体   中英

CAST not valid (OLEDB Date)

good day. I am using an OLEDB connection to connect to DB2 database. I am having problems mapping the dates from database inside .NET.

In my business object I defined a private DateTime _genftmdpdate=DateTime.MinValue; But whenever I fetch the date from database and populate inside my variable I get Specified Cast not valid error

myftModInstall.genFtMDPDate = myRecord.GetDateTime(myRecord.GetOrdinal("GENFTMDPDATE"))

Please help me as I don't want to convert the date into string all the time.

Edit --- This won't work as well myftModInstall.genFtMDPDate = Convert.ToDateTime(myRecord.GetType((System.Data.OleDb.OleDbType.Date)(myRecord.GetOrdinal("GENFTMDPDATE"))));

The IBMDA* providers default to mapping DB2 Date data types to strings. You must override the default in your connection string:

convert date time to char=FALSE

See this question: Using AS400 date in SSRS Report

IBM support article on connection properties: http://www-01.ibm.com/support/docview.wss?uid=nas8N1017400

Make sure that you check for a NULL before attempting to retrieve a value.

if(!myRecord.IsDBNull(myRecord.GetOrdinal("GENFTMDPDATE"))
    myftModInstall.genFtMDPDate = myRecord.GetDateTime(myRecord.GetOrdinal("GENFTMDPDATE"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM