SQLDataReader只返回查询返回1行的内容

时间:2014-06-25 19:14:08

标签: c# sql visual-studio-2008

我有一个SQLQuery,SQLDataReader不能可靠地返回结果。

这是查询:

SELECT b.ID,b.VERANSTALTER, b.SERIENBOOKUID, (select oa.ORIGINALDATE from OUTLOOKADDIN oa where oa.BOOKINGID = b.ID and oa.GUID is null) as ORIGINALDATE , rr.VON ,rr.BIS, b.THEMA, b.STORNO, ra.BEZEICHNUNG, et.BEZEICHNUNG as ETAGE,geb.ADRESSE, st.NAME as STADT 
from BUCHUNG b 
JOIN RESERVIERUNGRAUM rr on rr.BUCHUNG_ID = b.ID 
JOIN RAUM ra on ra.ID = rr.RAUM_ID 
JOIN ETAGE et on et.ID = ra.ETAGE_ID 
JOIN GEBAEUDE geb on geb.ID = ra.GEBAEUDE_ID 
JOIN STADT st on st.ID = ra.STADT_ID 
WHERE b.UPDATE_DATE BETWEEN cast('20140625 20:58:16' as DATETIME) AND getdate() 
AND b.BOOKVERNR = 0 
AND b.ID = (select distinct oa.BOOKINGID from OUTLOOKADDIN oa where b.ID = oa.BOOKINGID and oa.GUID is null)

当我向SQLManagement字段输入Query时,它返回一个结果。

这是主要功能

  public void start()
            {
                String SQLQuery = "SELECT b.ID,b.VERANSTALTER, b.SERIENBOOKUID, (select oa.ORIGINALDATE from OUTLOOKADDIN oa where oa.BOOKINGID = b.ID and oa.GUID is null) as ORIGINALDATE , rr.VON ,rr.BIS, b.THEMA, b.STORNO, ra.BEZEICHNUNG, et.BEZEICHNUNG as ETAGE,geb.ADRESSE, st.NAME as STADT"
                                + " from BUCHUNG b"
                                + " JOIN RESERVIERUNGRAUM rr on rr.BUCHUNG_ID = b.ID"
                                + " JOIN RAUM ra on ra.ID = rr.RAUM_ID"
                                + " JOIN ETAGE et on et.ID = ra.ETAGE_ID"
                                + " JOIN GEBAEUDE geb on geb.ID = ra.GEBAEUDE_ID"
                                + " JOIN STADT st on st.ID = ra.STADT_ID"
                                + " WHERE b.UPDATE_DATE BETWEEN " + oh.DatToDBISO(oldDate) + " AND getdate() AND b.BOOKVERNR = 0"
                                + " AND b.ID = (select distinct oa.BOOKINGID from OUTLOOKADDIN oa where b.ID = oa.BOOKINGID and oa.GUID is null)";
                SqlDataReader reader = oh.getSQLDR(SQLQuery);

                List<BookingUpdate> bookingUpdateList = new List<BookingUpdate>();

                if (reader.HasRows)
                {
                log.Debug("not empty");
                }
                else
                {
                log.Debug("empty");
                }
                while (reader.Read())
                {
                    BookingUpdate buObject = new BookingUpdate();
                    Int32 SerienBookUID = 0; ;

                    if (reader["SERIENBOOKUID"] != DBNull.Value)
                    {
                    log.Debug("b2");
                        SerienBookUID = Convert.ToInt32(reader["SERIENBOOKUID"]);
                    }

...
}

这是连接到SQL的Helper函数

 public SqlDataReader getSQLDR(string query)
    {
        SqlConnection con = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand(query, con);
        try
        {
            cmd.Connection = con;
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        catch (Exception ex)
        {
            log.Debug("Fehler beim übergeben des SQLDATAReaders :" + ex.Message);
        }
        return null;

    }

1 个答案:

答案 0 :(得分:3)

您的问题是将查询与日期联系起来。如果要使用串联,那么日期值应该用单引号括起来,所以这个:

b.UPDATE_DATE BETWEEN " + oh.DatToDBISO(oldDate) + " AND getdate() ....

应该是

b.UPDATE_DATE BETWEEN '" + oh.DatToDBISO(oldDate) + "' AND getdate() 

更好的选择将使用SqlParameter并将该参数与您的查询一起传递,这不仅可以让您免于SQL Injection,还可以让您通过日期对象而不用担心它的表示格式。