如何使用SqlDataReader从C#中的SQL Server数据库检索多行?

时间:2018-12-16 20:37:35

标签: c# sql sql-server sqldatareader

我正在尝试编写一种方法,该方法应该从数据库中的表中检索多行,并使用这些数据实例化多个对象。但是,据我所知,数据库仅返回第一行。当我这样做时:

public static List<Event> getMultipleEvents(string[] eventNames)
        {
            List<Event> rtnList = new List<Event>(); 

            string EventsToRetrieve = "";

            foreach (var item in eventNames)
            {
                if (EventsToRetrieve != "")
                {
                    EventsToRetrieve += " OR ";
                }

                EventsToRetrieve += "eventName = '";
                EventsToRetrieve += item;
                EventsToRetrieve += "' ";
            }
            // This is the string that the method constructs based on the input i am testing with 
            //"eventName = 'event six'  OR eventName = ' event two'  OR eventName = ' event one'  OR eventName = ' event seven' "
            using (SqlConnection sqlConnection = Globals.GetSqlConnection())
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand("SELECT * FROM questions WHERE " + EventsToRetrieve + ";", sqlConnection))
                {
                    using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        if (sqlDataReader != null)
                        {
                                while (sqlDataReader.Read())
                                {
                                    Event newEvent = new Event("", DateTime.MinValue, DateTime.MinValue);

                                    string startDateTimeStringFromDB = sqlDataReader["startDateDay"].ToString() + "-" + sqlDataReader["startDateMonth"].ToString() + "-" + sqlDataReader["startDateYear"].ToString();
                                    string endDateTimeStringFromDB = sqlDataReader["endDateDay"].ToString() + "-" + sqlDataReader["endDateMonth"].ToString() + "-" + sqlDataReader["endDateYear"].ToString();

                                    newEvent.EventName = sqlDataReader["eventName"].ToString();

                                    if (DateTime.TryParse(startDateTimeStringFromDB, out DateTime startDateTime))
                                    {
                                        newEvent.StartDate = startDateTime;
                                    }

                                    if (DateTime.TryParse(endDateTimeStringFromDB, out DateTime endDateTime))
                                    {
                                        newEvent.EndDate = endDateTime;
                                    }
                                    rtnList.Add(newEvent);
                                }
                        }
                    }
                }
            }
            return rtnList;
        }

有人可以向我解释我做错了什么吗?我还尝试按照此处How to read multiple resultset from SqlDataReader?的建议将while循环包装在do while循环中,但是它没有任何改变。

1 个答案:

答案 0 :(得分:2)

您的代码似乎没有任何错误。但是,我相信您的查询有错误。

首先,在构建SQL查询时永远不要使用字符串连接。而是使用Parameterized Queries

使用参数化查询,由于它不包含条件字符串连接,因此调试SQL语句甚至更加容易。