错误的日期格式C#,Oracle

时间:2013-02-11 08:26:31

标签: c# oracle

我正在尝试以正确的格式(dd / mm / yyyy)获取日期。目前它采用以下格式: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];
}

然后我还有一个上一个按钮和下一个按钮......

2 个答案:

答案 0 :(得分:6)

您的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语句 - 您应该始终确保清理与数据库相关的资源。)

答案 1 :(得分:0)

您的第一列没有名称,因此您无法使用"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"]));