SqlDataReader stores everything as string?

I am trying to retrieve data using SqlDataReader, one column of which is in datetime. I would like to do something like this

SqlCommand command = new SqlCommand("SELECT * FROM table", connection); //connection is defined earlier
        SqlDataReader data = command.ExecuteReader();
        while(data.Read()){
             DateTime birthday = data["Birth"];
             list.Add(birthday);
        }
    }

      

Can I do it? Or does the SqlDataReader return strings, in which case I would need to create a new DateTime object using that string?

Thanks, -S

+1


a source to share


2 answers


SqlDataReader returns data as strongly typed objects - just call the correct method like:



data.GetDateTime (ordinal)

+7


a source


Do you want to:

DateTime birthday = data.GetDateTime(data.GetOrdinal("Birth"));

      



SqlDataReader has a bunch of strongly typed Get * () methods.

+4


a source







All Articles