为什么.commandText不像在sqldeveloper中的Query中那样识别LIKE语句?

时间:2014-09-26 10:20:56

标签: c# mysql sql oracle

我正在使用CommandText从数据库中获取一些数据,基本上我有三个参数从服务传递,其值为:

ORIG_LOC = STPX

PLAN_WRK_ORIG_DEp = 18:15

uwttid = 631D62MW26

这一切都很好,我有这个命令文本声明。

 cmd.CommandText = "SELECT * FROM RESERVED_SEATS WHERE TRAIN_ID = (SELECT DISTINCT TRAIN_ID FROM DAILY_SCHEDULES WHERE ORIG_LOC ='" + ORIG_LOC + "' AND UWTTID ='" + uwttid + "' AND PLAN_WRK_ORIG_DEP LIKE '%" + PLAN_WRK_ORIG_DEP + "%')";

似乎只返回null并且没有给我任何结果,但是如果我摆脱了最后一部分而只是使用它:

cmd.CommandText = "SELECT * FROM RESERVED_SEATS WHERE TRAIN_ID = (SELECT DISTINCT TRAIN_ID FROM DAILY_SCHEDULES WHERE ORIG_LOC ='" + ORIG_LOC + "' AND UWTTID ='" + uwttid + "')";

它返回所有必要的数据。这很令人沮丧,因为这个命令在sqldeveloper中工作并返回所有必要的表,但是当我在顶部的语句中使用它时,变量的值与我在下面设置的值相同时,它不会返回任何结果。它有LIKE语句的问题,我不知道为什么。

下面的语句是我可以在sqldeveloper中使用来查询我的数据库,它返回我需要的表的确切结果,但是当在.CommandText中使用CKE中的LIKE时,它只是不会像它一样搜索它在sqldeveloper中查询它。

SELECT * FROM RESERVED_SEATS rs WHERE rs.TRAIN_ID = (SELECT DISTINCT ds.TRAIN_ID FROM DAILY_SCHEDULES ds WHERE ds.UWTTID = '631D62MW26' AND ds.ORIG_LOC = 'STPX' AND ds.PLAN_WRK_ORIG_DEP LIKE '%18:15%');

在这里你可以看到我在CommandText中传递的变量具有相同的值,并且2/3工作但是当我尝试对PLAN_WRK_ORIG_DEP使用LIKE语句时它只是不起作用。

enter image description here

这是我试图在我的Oracle数据库中查询的行,因此您可以看到它确实存在。 enter image description here

概要版本 所以基本上如果这是令人困惑的并且没有意义,我真正要问的是。为什么这个陈述不起作用?

cmd.CommandText = "SELECT * FROM RESERVED_SEATS WHERE TRAIN_ID = (SELECT DISTINCT TRAIN_ID FROM DAILY_SCHEDULES WHERE ORIG_LOC ='" + ORIG_LOC + "' AND UWTTID ='" + uwttid + "' AND PLAN_WRK_ORIG_DEP LIKE '%" + PLAN_WRK_ORIG_DEP + "%')";

当这个相同的SQLDEVELOPER作为查询工作时?

 SELECT * FROM RESERVED_SEATS rs WHERE rs.TRAIN_ID = (SELECT ds.TRAIN_ID FROM DAILY_SCHEDULES ds WHERE ds.UWTTID = '631D62MW26' AND ds.ORIG_LOC = 'STPX' AND ds.PLAN_WRK_ORIG_DEP LIKE '%18:15%');

不确定是否相关,但这是它正在使用的方法:当我摆脱PLAN_WRK_ORIG_DEP部分但在使用它时不起作用时,此方法有效。

  public ServiceResponse GetReservations(string uwttid, string ORIG_LOC, string PLAN_WRK_ORIG_DEP)
    {
        ServiceResponse sR = new ServiceResponse();

        JavaScriptSerializer jscript = new JavaScriptSerializer();

        try
        {
            string dataSource = "Data Source=GMOB; User Id=Firenze; Password=Dumbledore";
            OracleConnection conn = new OracleConnection(dataSource);
            List<Reservations> reservationsList = new List<Reservations>();
            conn.Open();


                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                cmd.CommandText = "SELECT * FROM RESERVED_SEATS WHERE TRAIN_ID = (SELECT DISTINCT TRAIN_ID FROM DAILY_SCHEDULES WHERE ORIG_LOC ='" + ORIG_LOC + "' AND UWTTID ='" + uwttid + "' AND PLAN_WRK_ORIG_DEP LIKE '%" + PLAN_WRK_ORIG_DEP + "%')";


                cmd.CommandType = CommandType.Text;
                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Reservations reservations = new Reservations();
                    var spacing = " - ";
                    var bracket1 = " (";
                    var bracket2 = ")";
                    reservations.Coach = dr["COACH_LETTER"].ToString();
                    reservations.Seat = dr["SEAT_NUM"].ToString() + dr["SEAT_TYPE"].ToString() + bracket1 + dr["POSITION"].ToString() + bracket2;
                    reservations.Class = dr["CLASS"].ToString();
                    reservations.Leg = dr["LOC_START"].ToString() + spacing + dr["LOC_END"].ToString();
                    reservations.Notes = dr["NOTES"].ToString();
                    reservationsList.Add(reservations);
                }
                dr.Close();
                conn.Dispose();

            sR.DataResponse = jscript.Serialize(reservationsList);
            return sR;

        }
        catch (Exception ex)
        {
            Utilities.LogError("getreservations" + ex.ToString());
            sR.Message = "Failed";
            return sR;
        }


    }

2 个答案:

答案 0 :(得分:0)

不是回答,但请,使用参数(和using):

List<Reservations> reservationsList = new List<Reservations>();
using(OracleConnection conn = new OracleConnection(dataSource))
using(OracleCommand cmd = new OracleCommand())
{
    conn.Open();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    cmd.BindByName = true;
    cmd.CommandText = "SELECT * FROM RESERVED_SEATS WHERE TRAIN_ID = (SELECT DISTINCT TRAIN_ID FROM DAILY_SCHEDULES WHERE ORIG_LOC = :ORIG_LOC AND UWTTID = :UWTTID AND PLAN_WRK_ORIG_DEP LIKE '%' + :PLAN_WRK_ORIG_DEP + '%')";
    cmd.Parameters.AddWithValue("ORIG_LOC", ORIG_LOC);
    cmd.Parameters.AddWithValue("UWTTID", uwttid);
    cmd.Parameters.AddWithValue("PLAN_WRK_ORIG_DEP", PLAN_WRK_ORIG_DEP);

    using(OracleDataReader dr = cmd.ExecuteReader())
    {        
        while (dr.Read()) {...}
    }
}
sR.DataResponse = jscript.Serialize(reservationsList);

答案 1 :(得分:0)

我尝试创建一个示例查询,以确保c#部分正确:
您也可以测试一下,看看是否可以毫无问题地运行它:
在这里,我尝试使用如下命令选择:
第一种方法,不建议:

    using (OracleConnection con = new OracleConnection(YourConnectionString))
    {
        using (OracleCommand cmd = new OracleCommand())
        {
            string query = string.Format("select * from table1 where id3 LIKE '{0}'", "%18:15%");
            cmd.CommandText = query;
            cmd.Connection = con;
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            var tb = new DataTable();
            da.Fill(tb);

        }
    }  

这选择很好。 现在如果您需要使用参数,请以这种方式使用它:

  using (OracleConnection con = new OracleConnection(YourConnectionString))
    {
        using (OracleCommand cmd = new OracleCommand())
        {
            string query = "select * from table1 where id3 LIKE :var";
            cmd.CommandText = query;
            cmd.Parameters.Add("var", "%18:15%");
            cmd.Connection = con;

            OracleDataAdapter da = new OracleDataAdapter(cmd);
            var tb = new DataTable();
            da.Fill(tb);

        }
    }  

运行也很好。

这是我用来测试它的示例数据查询:

CREATE TABLE test.TABLE1 (
  ID  NUMBER(10, 0),
  ID2 NVARCHAR2(50),
  ID3 NVARCHAR2(50)
)
TABLESPACE USERS
STORAGE (INITIAL 64 K
         NEXT 1 M
         MAXEXTENTS UNLIMITED)
LOGGING;

编辑: 如果你的问题是将你的字符串转换为DateTime变量,你可以像这样使用DateTime.Parse()

DateTime dt = DateTime.Parse("18:15");

这将为您提供一个日期时间变量,该变量包含:

9/26/2014 18:15:00

如果您正在处理非标准日期时间字符串

,您可能还会查看DateTime.ParseExact()