简体   繁体   中英

C# retrieve simple data from Oracle 11g return null

new to C#, the following is a simple code to connect Oracle 11g, retrieve sysdate, connection is ok, but sysdate is null, what am I missing? Please help, thank you very much. - mz

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            string oradb = "Data Source=localhost/xe; user Id=hr; Password=hr;";
            OracleConnection conn = new OracleConnection(oradb);
            conn.Open();
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select sysdate from dual";
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
            dr.Read();
            System.Console.WriteLine(dr.GetString(0));
            conn.Close();
            System.Console.ReadLine();
        }
    }
}

Retruning DataType is DateTime.

use dr.GetDateTime(0) instead of dr.GetString(0)

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