C#查询带引号的SQL命令

时间:2015-07-03 09:51:32

标签: c# sql database oracle

我想从c#中获取Oracle数据库中的数据。问题是名为“DATE”和“HOUR”的两列是SQL / Oracle表达式。

这是我的查询命令:

command.CommandText = @"SELECT BOXID, PUMP_BIT, DATE, HOUR, RUN_DURATION, POWER_ONS FROM PUMP_AGG_HOURLY WHERE BOXID = " + tb_BoxIDAktuell.Text + " AND PUMP_BIT = " + BitPumpe1 + " ORDER BY DATE, HOUR";

为了测试,我在DATE和HOUR列中包含引号,SQL语句在Oracle SQL Developer中运行:

SELECT BOXID, PUMP_BIT, "DATE", "HOUR", RUN_DURATION, POWER_ONS FROM PUMP_AGG_HOURLY WHERE BOXID = '4' AND PUMP_BIT = '1' ORDER BY "DATE", "HOUR";

在C#中,我添加了双引号,但声明给出了错误

command.CommandText = @"SELECT BOXID, PUMP_BIT, ""DATE"", ""HOUR"", RUN_DURATION, POWER_ONS FROM TPT2000_PUMP_AGG_HOURLY WHERE BOXID = " + tb_BoxIDAktuell.Text + " AND PUMP_BIT = " + BitPumpe1 + " AND DATE BETWEEN to_date('" + Start + "','dd/mm/yyyy hh24:mi:ss') and to_date('" + Ende + "','dd/mm/yyyy hh24:mi:ss') ORDER BY ""DATE"", ""HOUR""";
  

ORA-00936缺少表达

我认为这是因为ORDER BY DATE, HOUR是SQL表达式。我尝试了ORDER BY PUMP_AGG_HOURLY.DATE, PUMP_AGG_HOURLY.HOUR,但得到了同样的错误。

我该如何解决这个问题?感谢

2 个答案:

答案 0 :(得分:3)

这种语法对我有用:

$query = "SELECT ad.company, ad.po_box, 
                 ad.town, ld.address_id, 
                 ld.attn, ld.create_date,
                 ld.ref_no, ld.refference, 
                 ld.letter_body, ld.print_date 
          FROM letter_details ld 
          left join address_details ad ON (ad.id = ld.address_id)
          left join signatories s ON (ld.id = s.id) 
          WHERE ld.id='" .$_GET['id'] . "'";

Oracle测试表定义和示例数据:

using (connection)
{
    OracleCommand command = new OracleCommand();
    command.Connection = connection;
    command.CommandType = CommandType.Text;
    command.BindByName = true;
    command.CommandText =
        "SELECT BOXID, PUMP_BIT, \"DATE\", \"HOUR\", RUN_DURATION, POWER_ONS " +
        "  FROM PUMP_AGG_HOURLY " +
        "  WHERE BOXID = :BoxID AND PUMP_BIT = :BitPumpe " +
        "    AND \"DATE\" BETWEEN to_date(:Date1,'dd/mm/yyyy hh24:mi:ss') " +
        "                     and to_date(:Date2,'dd/mm/yyyy hh24:mi:ss')" +
        "  ORDER BY \"DATE\", \"HOUR\"";
    command.Parameters.Add("BoxID", '1');
    command.Parameters.Add("BitPumpe", '4');
    command.Parameters.Add("Date1", "30/01/2015 01:00:00");
    command.Parameters.Add("Date2", "30/01/2015 18:00:00");
    OracleDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(reader.GetString(0) + ", " + reader.GetString(1));
        Console.WriteLine(reader.GetDateTime(2));
    }
}

答案 1 :(得分:0)

这已在here

中得到解答

简短回答是:Oracle似乎使用双引号(“,例如”表格“),显然需要正确的案例