简体   繁体   中英

How to retrieve multiple rows from SQL Server database in C# using SqlDataReader?

I am trying to write a method which is supposed to retrieve multiple rows from a table in a database and use those data to instantiate a number of objects. However, as far as i can tell the database only returns the first row. When i do this:

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;
        }

Can anyone explain to me what i am doing wrong ? I Also tried wrapping the while loop in a do while loop as suggested here How to read multiple resultset from SqlDataReader? but it didn't change anything.

It doesn't seem to have any error in your code. however I believe that your query has errors.

First of all never ever use string concatenation when you are building your SQL Query. Instead use Parameterized Queries .

with the parameterized queries, it is even easier to debug your SQL statement since it does not include conditional string concatenations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM