1

I have a database named testDB, which contains table Versions, which contains a column [Release Date] with datetime format.

Now, I want to read it in my C# Windows Service:

protected void SqlConnect()
{
    SqlCommand comSql;
    DateTime relDate;
    SqlDataReader myReader = null;
    using (SqlConnection myConnection = new SqlConnection(_server + 
                                                 _username +
                                                 _password +
                                                 "Trusted_Connection=yes;" +
                                                 "database=testDB; " +
                                                 "connection timeout=30"))
    {
        try
        {
            myConnection.Open();
            comSql = new SqlCommand("select [Release Date] from dbo.Version",
                                                             myConnection);
            myReader = comSql.ExecuteReader();
            while (myReader.Read())
            {
                //Here's my problem, explained below
            }
        }
        catch 
        {

        }
        finally
        {
            if (myReader != null) myReader.Close();
        }

    }

}

Now, I want to assign the value stored in that column to relDate variable. However

relDate = myReader.GetDateTime();

requires GetDateTime to have column number passed there (if I understand this right). But I already selected column in my comSql. Is this the correct way to deal with this problem, ie. just putting the column number in the code?

EDIT: Ok judging by the answers I might word this question wrong or something.

I know that I must pass the column index to GetDateTime(). I ask if there's a way to do that without hardcoding it like GetDateTime(0).

5
  • why don't you just do reader["Release Date"] and do the conversion afterwards? Commented Jul 29, 2015 at 9:21
  • you could build a wrapper for the SqlDataReader which is able to do what you want Commented Jul 29, 2015 at 9:23
  • And also two try catch blocks , are redundant here. Commented Jul 29, 2015 at 9:29
  • I posted a small edit to my question. Commented Jul 29, 2015 at 9:30
  • Read about GetOrdinal method as pointed by @zoran. Commented Jul 29, 2015 at 9:33

1 Answer 1

4

You can use GetOrdinal method on the data reader to get ordinal of the column from its string name. In that way, you won't have to hardcode the column index.

GetOrdinal is also useful when you're reading data from the data reader in a loop. You can initialize the index variable before the loop starts and then use it in every iteration of the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.