来自select查询的数据的24格式

时间:2011-07-15 18:33:04

标签: c# date ms-access visual-studio-2008 time

我在C ++中运行查询,但我无法正确处理日期。我希望约会以24小时的日期时间格式返回(年/月/日yhy:Hh24:mi:ss)。我在MS Access中运行查询,如下所示:

SELECT Format(Data.[Date / Time],'mm/dd/yyyy hh:nn:ss'), DATA.[Hot Strip Mill Total], DATA.[Basic Oxygen Furnace Total], DATA.[Electro-Arc Furnace Total], DATA.[J-9 Shop Total], DATA.[Levy Maintence Building Total], DATA.[Ford Body Shop Total], DATA.[Ford Chiller Building Total], DATA.[Ford Dearborn W Plant Total], DATA.[Ford Dearborn E Plant Total], DATA.[Ford Dearborn Balcony Total], DATA.[Ford Final Assembly Total], DATA.[Ford Frame Plant Total], DATA.[Ford Dearborn N Plant Total], DATA.[Ford Tool and Die Total], DATA.[Ford Paint Plant Total], DATA.[Ford Glass Plant Total], DATA.[Hot Strip Mill Rate], DATA.[Basic Oxygen Furnace Rate], DATA.[Electro-Arc Furnace Rate], DATA.[J-9 Shop Rate], DATA.[Levy Maintence Building Rate], DATA.[Ford Body Shop Rate], DATA.[Ford Chiller Building Rate], DATA.[Ford Dearborn W Plant Rate], DATA.[Ford Dearborn E Plant Rate], DATA.[Ford Dearborn Balcony Rate], DATA.[Ford Final Assembly Rate], DATA.[Ford Frame Plant Rate], DATA.[Ford Dearborn N Plant Rate], DATA.[Ford Tool and Die Rate], DATA.[Ford Paint Plant Rate], DATA.[Ford Glass Plant Rate]
FROM DATA;

哪个运行正常;但是当我在我的代码中运行它时,我收到以下错误:

System.IndexOutOfRangeException:日期/时间    在System.Data.ProviderBase.FieldNameLookUp.GetOrdinal(String fieldName)

at system.Data.Odbc.OdbcDataReader.GetOrdinal(String value)

at System.Data.Odbc.OdbcDataReader.get_Item(String value)

...在我的代码的第120行

代码有人帮助我理解这一点。这是我现在必须处理的代码的最后一个组件,我真的很想完成它,谢谢你的所有帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Data.Odbc;

namespace ConsoleApplication7
{
class Program
{
    static void Main(string[] args)
    {
        string connectionString = "Dsn=Gas_meter_check";

        string dayTime = "";
        string hsmt = "";
        string boft = "";
        string eaft = "";
        string j9st = "";
        string lmbt = "";
        string fbst = "";
        string fcbt = "";
        string fdwpt ="";

        string fdept = "";
        string fdbt = "";
        string ffat = "";
        string ffpt = "";
        string fdnpt = "";
        string ftdt = "";
        string fppt = "";
        string fgpt = "";

        string hsmr = "";
        string bofr = "";
        string eafr = "";
        string j9sr = "";
        string lmbr = "";
        string fbsr = "";
        string fcbr = "";
        string fdwpr = "";

        string fdepr = "";
        string fdbr = "";
        string ffar = "";
        string ffpr = "";
        string fdnpr = "";
        string ftdr = "";
        string fppr = "";
        string fgpr = "";

        string sqlins = "";
        string connString = "DSN=Gas_Reporter_proj;Uid=rouge;Pwd=steel";
        OdbcConnection conn = new OdbcConnection(connString);

        OdbcConnection DbConnection = new OdbcConnection(connectionString);
        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbConnection.Open();
        //DbCommand.CommandText = "SELECT Data.[Date / Time],Data.[Basic Oxygen Furnace Total],Data.[Electro-Arc Furnace Total],Data.[J-9 Shop Total],Data.[Levy Maintence Building Total],Data.[Ford Body Shop Total], Data.[Ford Chiller Building Total],Data.[Ford Dearborn W Plant Total],Data.[Ford Dearborn E Plant Total], Data.[Ford Dearborn Balcony Total], Data.[Ford Final Assembly Total],Data.[Ford Frame Plant Total], Data.[Ford Dearborn N Plant Total],Data.[Ford Tool and Die Total], Data.[Ford Paint Plant Total],Data.[Ford Glass Plant Total], Data.[Basic Oxygen Furnace Rate], Data.[Electro-Arc Furnace Rate],Data.[J-9 Shop Rate],Data.[Levy Maintence Building Rate],Data.[Ford Body Shop Rate],Data.[Ford Chiller Building Rate],Data.[Ford Dearborn W Plant Rate],Data.[Ford Dearborn E Plant Rate],Data.[Ford Dearborn Balcony Rate],Data.[Ford Final Assembly Rate],Data.[Ford Frame Plant Rate],Data.[Ford Dearborn N Plant Rate],Data.[Ford Tool and Die Rate],Data.[Ford Paint Plant Rate],Data.[Ford Glass Plant Rate]FROM DATA";
        //THIS IS FOR WHEN THE THE INITIAL DATA IS UPLOADED
        //DbCommand.CommandText = "SELECT Data.[Date / Time],Data.[Basic Oxygen Furnace Total],Data.[Electro-Arc Furnace Total],Data.[J-9 Shop Total],Data.[Levy Maintence Building Total],Data.[Ford Body Shop Total], Data.[Ford Chiller Building Total],Data.[Ford Dearborn W Plant Total],Data.[Ford Dearborn E Plant Total], Data.[Ford Dearborn Balcony Total], Data.[Ford Final Assembly Total],Data.[Ford Frame Plant Total], Data.[Ford Dearborn N Plant Total],Data.[Ford Tool and Die Total], Data.[Ford Paint Plant Total],Data.[Ford Glass Plant Total], Data.[Basic Oxygen Furnace Rate], Data.[Electro-Arc Furnace Rate],Data.[J-9 Shop Rate],Data.[Levy Maintence Building Rate],Data.[Ford Body Shop Rate],Data.[Ford Chiller Building Rate],Data.[Ford Dearborn W Plant Rate],Data.[Ford Dearborn E Plant Rate],Data.[Ford Dearborn Balcony Rate],Data.[Ford Final Assembly Rate],Data.[Ford Frame Plant Rate],Data.[Ford Dearborn N Plant Rate],Data.[Ford Tool and Die Rate],Data.[Ford Paint Plant Rate],Data.[Ford Glass Plant Rate]FROM DATA WHERE Format( Data.[Date / Time], 'mm/dd/yyyy') >=(select Format(max( Data.[Date / Time]),'mm/dd/yyyy') from DATA)";
        // THIS IS FOR WHEN THE HOT MILL WORKING;
        DbCommand.CommandText = "SELECT Format(Data.[Date / Time],'mm/dd/yyyy hh:nn:ss'),DATA.[Hot Strip Mill Total],Data.[Basic Oxygen Furnace Total],Data.[Electro-Arc Furnace Total],Data.[J-9 Shop Total],Data.[Levy Maintence Building Total],Data.[Ford Body Shop Total], Data.[Ford Chiller Building Total],Data.[Ford Dearborn W Plant Total],Data.[Ford Dearborn E Plant Total], Data.[Ford Dearborn Balcony Total], Data.[Ford Final Assembly Total],Data.[Ford Frame Plant Total], Data.[Ford Dearborn N Plant Total],Data.[Ford Tool and Die Total], Data.[Ford Paint Plant Total],Data.[Ford Glass Plant Total], DATA.[Hot Strip Mill Rate], Data.[Basic Oxygen Furnace Rate], Data.[Electro-Arc Furnace Rate],Data.[J-9 Shop Rate],Data.[Levy Maintence Building Rate],Data.[Ford Body Shop Rate],Data.[Ford Chiller Building Rate],Data.[Ford Dearborn W Plant Rate],Data.[Ford Dearborn E Plant Rate],Data.[Ford Dearborn Balcony Rate],Data.[Ford Final Assembly Rate],Data.[Ford Frame Plant Rate],Data.[Ford Dearborn N Plant Rate],Data.[Ford Tool and Die Rate],Data.[Ford Paint Plant Rate],Data.[Ford Glass Plant Rate]FROM DATA";
        //WHERE Format( Data.[Date / Time], 'mm/dd/yyyy') >=(select Format(max( Data.[Date / Time]),'mm/dd/yyyy') from DATA)
        OdbcDataReader DbReader = DbCommand.ExecuteReader();
        int fCount = DbReader.FieldCount;
 try
{
        OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);                            
        conn.Open();

        cmdnon.Parameters.Add(dayTime, OdbcType.DateTime);
        cmdnon.Parameters.Add(hsmt, OdbcType.Numeric);
        cmdnon.Parameters.Add(boft, OdbcType.Numeric);
        cmdnon.Parameters.Add(eaft, OdbcType.Numeric);
        cmdnon.Parameters.Add(j9st, OdbcType.Numeric);
        cmdnon.Parameters.Add(lmbt, OdbcType.Numeric);
        cmdnon.Parameters.Add(fbst, OdbcType.Numeric);
        cmdnon.Parameters.Add(fcbt, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdwpt, OdbcType.Numeric);

        cmdnon.Parameters.Add(fdept, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdbt, OdbcType.Numeric);
        cmdnon.Parameters.Add(ffat, OdbcType.Numeric);
        cmdnon.Parameters.Add(ffpt, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdnpt, OdbcType.Numeric);
        cmdnon.Parameters.Add(ftdt, OdbcType.Numeric);
        cmdnon.Parameters.Add(fppt, OdbcType.Numeric);
        cmdnon.Parameters.Add(fgpt, OdbcType.Numeric);

        cmdnon.Parameters.Add(hsmr, OdbcType.Numeric);
        cmdnon.Parameters.Add(bofr, OdbcType.Numeric);
        cmdnon.Parameters.Add(eafr, OdbcType.Numeric);
        cmdnon.Parameters.Add(j9sr, OdbcType.Numeric);
        cmdnon.Parameters.Add(lmbr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fbsr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fcbr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdwpr, OdbcType.Numeric);

        cmdnon.Parameters.Add(fdepr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdbr, OdbcType.Numeric);
        cmdnon.Parameters.Add(ffar, OdbcType.Numeric);
        cmdnon.Parameters.Add(ffpr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fdnpr, OdbcType.Numeric);
        cmdnon.Parameters.Add(ftdr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fppr, OdbcType.Numeric);
        cmdnon.Parameters.Add(fgpr, OdbcType.Numeric);




        while (DbReader.Read())
                    {
                        dayTime = DbReader["Date / Time"].ToString();
                        hsmt = DbReader["Hot Strip Mill Total"].ToString();     
                        boft = DbReader["Basic Oxygen Furnace Total"].ToString();   
                        eaft = DbReader["Electro-Arc Furnace Total"].ToString(); 
                        j9st = DbReader["J-9 Shop Total"].ToString();
                        lmbt = DbReader["Levy Maintence Building Total"].ToString();
                        fbst = DbReader["Ford Body Shop Total"].ToString();    
                        fcbt = DbReader["Ford Chiller Building Total"].ToString(); 
                        fdwpt = DbReader["Ford Dearborn W Plant Total"].ToString();  


                        fdept = DbReader["Ford Dearborn E Plant Total"].ToString();
                        fdbt = DbReader["Ford Dearborn Balcony Total"].ToString();    
                        ffat = DbReader["Ford Final Assembly Total"].ToString();  
                        ffpt = DbReader["Ford Frame Plant Total"].ToString();  
                        fdnpt = DbReader["Ford Dearborn N Plant Total"].ToString();
                        ftdt = DbReader["Ford Tool and Die Total"].ToString();    
                        fppt = DbReader["Ford Paint Plant Total"].ToString();    
                        fgpt = DbReader["Ford Glass Plant Total"].ToString();  

                        hsmr = DbReader["Hot Strip Mill Rate"].ToString();
                        bofr = DbReader["Basic Oxygen Furnace Rate"].ToString();   
                        eafr = DbReader["Electro-Arc Furnace Rate"].ToString(); 
                        j9sr = DbReader["J-9 Shop Rate"].ToString();          
                        lmbr = DbReader["Levy Maintence Building Rate"].ToString();                            
                        fbsr = DbReader["Ford Body Shop Rate"].ToString();   
                        fcbr = DbReader["Ford Chiller Building Rate"].ToString();
                        fdwpr = DbReader["Ford Dearborn W Plant Rate"].ToString();

                        fdepr = DbReader["Ford Dearborn E Plant Rate"].ToString();
                        fdbr = DbReader["Ford Dearborn Balcony Rate"].ToString();   
                        ffar = DbReader["Ford Final Assembly Rate"].ToString();   
                        ffpr = DbReader["Ford Frame Plant Rate"].ToString();     
                        fdnpr = DbReader["Ford Dearborn N Plant Rate"].ToString(); 
                        ftdr = DbReader["Ford Tool and Die Rate"].ToString();     
                        fppr = DbReader["Ford Paint Plant Rate"].ToString();    
                        fgpr = DbReader["Ford Glass Plant Rate"].ToString();

                        //cmdnon.CommandText = "insert into NAT_GAS_READINGS(BOF_TOTAL,EAF_TOTAL,J9_SHOP_TOTAL,LEVY_TOTAL,BODY_SHOP_TOTAL,CHILLER_BLDG_TOTAL,WPLANT_TOTAL,EPLANT_TOTAL,BALCONY_TOTAL,FINAL_ASSEMBLY_TOTAL,FRAME_PLANT_TOTAL,NPLANT_TOTAL,TOOL_DIE_TOTAL,PAINT_PLANT_TOTAL,GLASS_PLANT_TOTAL,BOF,EAF,J9_SHOP,LEVY,BODY_SHOP,CHILLER_BLDG,WPLANT,EPLANT,BALCONY,FINAL_ASSEMBLY,FRAME_PLANT,NPLANT,TOOL_DIE,PAINT_PLANT,GLASS_PLANT,DATETIME) values ( to_number(1564),to_number(1385), to_number(87886),to_number(687489.25),to_number(9879.35),to_number(897.54),to_number(98754.38),to_number(768.47), to_number(654), to_number(897896),to_number(8978.18),to_number(897234), to_number(3546.28),to_number(54798),to_number(65496), to_number(6487), to_number(98796), to_number(6485), to_number(5488), to_number(65496),to_number(65465),to_number(1411), to_number(3354), to_number(54879),to_number(35),to_number(21),to_number(325),to_number(3215),to_number(3215),to_number(214))";
                        //cmdnon.CommandText = "insert into NAT_GAS_READINGS(BOF_TOTAL,EAF_TOTAL,J9_SHOP_TOTAL,LEVY_TOTAL,BODY_SHOP_TOTAL,CHILLER_BLDG_TOTAL,WPLANT_TOTAL,EPLANT_TOTAL,BALCONY_TOTAL,FINAL_ASSEMBLY_TOTAL,FRAME_PLANT_TOTAL,NPLANT_TOTAL,TOOL_DIE_TOTAL,PAINT_PLANT_TOTAL,GLASS_PLANT_TOTAL,BOF,EAF,J9_SHOP,LEVY,BODY_SHOP,CHILLER_BLDG,WPLANT,EPLANT,BALCONY,FINAL_ASSEMBLY,FRAME_PLANT,NPLANT,TOOL_DIE,PAINT_PLANT,GLASS_PLANT,DATETIME) values (to_number('" + boft + "'), to_number('" + eaft + "'), to_number('" + j9st + "'), to_number('" + lmbt + "'),to_number('" + fbst + "'),to_number('" + fcbt + "'),to_number('" + fdwpt + "'), to_number('" + fdept + "'), to_number('" + fdbt + "'), to_number('" + ffat + "'), to_number('" + ffpt + "'), to_number('" + fdnpt + "'), to_number('" + ftdt + "'), to_number('" + fppt + "'), to_number('" + fgpt + "'), to_number('" + bofr + "'), to_number('" + eafr + "'), to_number('" + j9sr + "'), to_number('" + lmbr + "'), to_number('" + fbsr + "'), to_number('" + fcbr + "'), to_number('" + fdwpr + "'), to_number('" + fdepr + "'),  to_number('" + fdbr + "'), to_number('" + ffar + "'), to_number('" + ffpr + "'), to_number('" + fdnpr + "'), to_number('" + ftdr + "'), to_number('" + fppr + "'),to_number('" + fgpr + "'),to_date('" + dayTime + "', 'MM/DD/YYYY HH:MI:SS AM' ))";
                        //cmdnon.CommandText = "insert into NAT_GAS_READINGS( HSM_TOTAL,BOF_TOTAL,EAF_T0TAL,J9_SHOP_TOTAL,LEVY_TOTAL,BODY_SHOP_TOTAL,CHILLER_BLDG_TOTAL,WPLANT_TOTAL,EPLANT_TOTAL,BALCONY_TOTAL,FINAL_ASSEMBLY_TOTAL,FRAME_PLANT_TOTAL,NPLANT_TOTAL,TOOL_DIE_TOTAL,PAINT_PLANT_TOTAL,GLASS_PLANT_TOTAL,HSM,BOF,EAF,J9_SHOP,LEVY,BODY_SHOP,CHILLER_BLDG,WPLANT,EPLANT,BALCONY,FINAL_ASSEMBLY,FRAME_PLANT,NPLANT,TOOL_DIE,PAINT_PLANT,GLASS_PLANT,DATETIME) values (to_number('" + hsmt + "'), to_number('" + boft + "'), to_number('" + eaft + "'), to_number('" + j9st + "'), to_number('" + lmbt + "'),to_number('" + fbst + "'),to_number('" + fcbt + "'), to_number('" + fdwpt + "'), to_number('" + fdept + "'), to_number('" + fdbt + "'), to_number('" + ffat + "'), to_number('" + ffpt + "'), to_number('" + fdnpt + "'), to_number('" + ftdt + "'), to_number('" + fppt + "'), to_number('" + fgpt + "'), to_number('" + hsmr + "'), to_number('" + bofr + "'), to_number('" + eafr + "'), to_number('" + j9sr + "'), to_number('" + lmbr + "'), to_number('" + fbsr + "'), to_number('" + fcbr + "'), to_number('" + fdwpr + "'), to_number('" + fdepr + "'), to_number('" + fdbr + "'), to_number('" + ffar + "'), to_number('" + ffpr + "'), to_number('" + fdnpr + "'), to_number('" + ftdr + "'), to_number('" + fppr + "'), to_number('" + fgpr + "'), to_date('" + dayTime + "', 'MM/DD/YYYY HH24:MI:SS' ))";
                        cmdnon.CommandText = "insert into NAT_GAS_READINGS( HSM_TOTAL,BOF_TOTAL,EAF_TOTAL,J9_SHOP_TOTAL,LEVY_TOTAL,BODY_SHOP_TOTAL,CHILLER_BLDG_TOTAL,WPLANT_TOTAL,EPLANT_TOTAL,BALCONY_TOTAL,FINAL_ASSEMBLY_TOTAL,FRAME_PLANT_TOTAL,NPLANT_TOTAL,TOOL_DIE_TOTAL,PAINT_PLANT_TOTAL,GLASS_PLANT_TOTAL,HSM,BOF,EAF,J9_SHOP,LEVY,BODY_SHOP,CHILLER_BLDG,WPLANT,EPLANT,BALCONY,FINAL_ASSEMBLY,FRAME_PLANT,NPLANT,TOOL_DIE,PAINT_PLANT,GLASS_PLANT,DATETIME) values (to_number('" + hsmt + "'), to_number('" + boft + "'), to_number('" + eaft + "'), to_number('" + j9st + "'), to_number('" + lmbt + "'),to_number('" + fbst + "'),to_number('" + fcbt + "'), to_number('" + fdwpt + "'), to_number('" + fdept + "'), to_number('" + fdbt + "'), to_number('" + ffat + "'), to_number('" + ffpt + "'), to_number('" + fdnpt + "'), to_number('" + ftdt + "'), to_number('" + fppt + "'), to_number('" + fgpt + "'), to_number('" + hsmr + "'), to_number('" + bofr + "'), to_number('" + eafr + "'), to_number('" + j9sr + "'), to_number('" + lmbr + "'), to_number('" + fbsr + "'), to_number('" + fcbr + "'), to_number('" + fdwpr + "'), to_number('" + fdepr + "'), to_number('" + fdbr + "'), to_number('" + ffar + "'), to_number('" + ffpr + "'), to_number('" + fdnpr + "'), to_number('" + ftdr + "'), to_number('" + fppr + "'), to_number('" + fgpr + "'), to_date('01/24/1987 23:54:12', 'MM/DD/YYYY HH24:MI:SS' ))";
                        int rowsAffected = cmdnon.ExecuteNonQuery();
                        Console.WriteLine(rowsAffected);

                    }


                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    conn.Close();                       
                }
   DbReader.Close();
   DbCommand.Dispose();
   DbConnection.Close();
    }

}
}

1 个答案:

答案 0 :(得分:0)

来自http://msdn.microsoft.com/en-us/library/90a7b8k5.aspx

OdbcDataReader :: Item

的描述
IndexOutOfRangeException : No column with the specified name was found. 

显然,命名数据库表列时存在问题。

提示:尝试先按字段编号获取字段。然后尝试OdbcDataReader::GetName(number)获取列名称。

编辑:

dayTime = DbReader.GetString(0); // or
dayTime = DbReader.GetString("Date / Time");

对我来说似乎更具可读性。