错误日期格式C#,Oracle

我试图以正确的格式(日/月/年)获取我的日期。 目前它的格式如下:MM-DD-YYYY HH24:MI:SS当我将其更改为dd / mm / yyyy时,它可以在数据库(Oracle)中运行。 只要我在我的应用程序中运行它,我会得到异常:IndexOutOfRange at:

this.InfoList9.Add(dr["start_rcv_datetime"].ToString());

请参阅下面的代码。

public List<String> InfoList = new List<String>();

private void populatelblDate()
{
    conn.Open();
    string query;
    query = "select to_char(dg.start_rcv_datetime,'dd/mm/yyyy') from dc_pallet dp, dc_pallet_stock dps , dc_grv dg , sku s ,prod_size ps,colour c ,purch_order_carton_sku pocs , dc_crane_instruc dci where dps.pallet_id_no = '" + palletId.ToString() + "' and dp.pallet_id_no = dps.pallet_id_no and dg.dc_grv_id_no = dps.dc_grv_id_no and dg.order_no = dps.order_no and dg.company_id_no = dps.company_id_no and s.company_id_no = dps.company_id_no and s.company_id_no = dg.company_id_no and dps.company_id_no = c.company_id_no and dps.company_id_no = ps.company_id_no and s.prod_size_id_no = ps.prod_size_id_no and s.colour_id_no = c.colour_id_no and dps.company_id_no = ps.company_id_no and pocs.order_no = dps.order_no and pocs.carton_code = dps.carton_code and pocs.company_id_no = dps.company_id_no and pocs.sku_id_no = s.sku_id_no and dci.pallet_id_no(+) = dp.pallet_id_no";

    OracleCommand cmd = new OracleCommand(query, conn);
    OracleDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        this.InfoList.Add(dr["start_rcv_datetime"].ToString());
    }
    dr.Close();
    conn.Close();
}

private void frmInfo_Load(object sender, EventArgs e)
{ 
    populatelblDate();
    lbl1.Text = this.InfoList[0];
}

然后我有一个prev和next按钮以及...


您的IndexOutOfRange异常表明,直接的问题是结果集不包含start_rcv_datetime列 - 可能是因为to_char转换。

不要在数据库端处理字符串。 将该值作为DateTime获取,然后在客户端以任何想要的格式对其进行格式化。

使用dr.GetDateTime获取该值,从查询中删除了to_char部分:

query = "select dg.start_rcv_datetime from ...";

using (OracleCommand cmd = new OracleCommand(query, conn))
{
    using (OracleDataReader dr = cmd.ExecuteReader())
    {
        int dateColumn = dr.GetOrdinal("start_rcv_datetime");
        while (dr.Read())
        {
            DateTime date = dr.GetDateTime(0);
            // Or whatever - consider cultural implications
            string text = date.ToString("dd/MM/yyyy");
            InfoList.Add(text);
        }
    }
}

(请注意using语句 - 您应该始终确保清理与数据库相关的资源。)


您的第一列没有名称,因此您无法使用"start_rcv_datetime"

直接的解决办法是改变sql来读取

query = "select to_char(dg.start_rcv_datetime,'dd/mm/yyyy') as start_rcv_datetime from dc_pallet dp, dc_pallet_stock dps , dc_grv dg , sku s ,prod_size ps,colour c ,purch_order_carton_sku pocs , dc_crane_instruc dci where dps.pallet_id_no = '" + palletId.ToString() + "' and dp.pallet_id_no = dps.pallet_id_no and dg.dc_grv_id_no = dps.dc_grv_id_no and dg.order_no = dps.order_no and dg.company_id_no = dps.company_id_no and s.company_id_no = dps.company_id_no and s.company_id_no = dg.company_id_no and dps.company_id_no = c.company_id_no and dps.company_id_no = ps.company_id_no and s.prod_size_id_no = ps.prod_size_id_no and s.colour_id_no = c.colour_id_no and dps.company_id_no = ps.company_id_no and pocs.order_no = dps.order_no and pocs.carton_code = dps.carton_code and pocs.company_id_no = dps.company_id_no and pocs.sku_id_no = s.sku_id_no and dci.pallet_id_no(+) = dp.pallet_id_no";

但是,您可以简单地从数据库中将日期作为日期时间返回,然后对结果使用string.Format

例如。

this.InfoList.Add(string.Format("{0:dd/MM/yyyy}", dr["start_rcv_datetime"]));
链接地址: http://www.djcxy.com/p/36911.html

上一篇: Wrong date format C#, Oracle

下一篇: Oracle — separate time and date