0

I have a SQL Server table like this:

userID int
xCoordinate int
yCoordinate int
recordTime time(7)
itemId int

I want to get all recording times of a given user (let's say user 1 for now). I used the following code for this:

public static void something()
{
    string stmt = "select * from GazeTable where id = " + 1 + " ;";

    SqlConnection conn = GetConnection();
    SqlCommand cmd = new SqlCommand(stmt, conn);

    conn.Open();

    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader.GetDateTime(3));
        }
    }

    conn.Close();
}

But it throws an error

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

saying, this assignment is not valid.

How can I print the time in terms of ("HH:mm:ss.ffff"), for each recordTime in my table?

5
  • Is it working to get y coordinate with reader.GetInt(2)? Commented Aug 18, 2016 at 8:21
  • 1
    If I'm not mistaken the SQL Type Time(7) does not convert to DateTime but TimeSpan in C#, so try reader.GetTimeSpan(3) instead Commented Aug 18, 2016 at 8:24
  • @ntohl yes it does Commented Aug 18, 2016 at 8:26
  • @BojanB oh my god it worked, everywhere i searched, kept saying, datetime is the equivalent of sql's time, thank you so much Commented Aug 18, 2016 at 8:26
  • @Danabey no prob :) Commented Aug 18, 2016 at 8:33

3 Answers 3

2

The SQL Type Time(7) is equivalent to the C# Type TimeSpan not DateTime, to get a DateTime Value the SQL Type has to be datetime.

So just change your code to read TimeSpan instead of DateTime:

public static void something()
{
   string stmt = "select * from GazeTable where id = " + 1 + " ;";
   SqlConnection conn = GetConnection();
   SqlCommand cmd = new SqlCommand(stmt, conn);

   conn.Open();
   using (var reader = cmd.ExecuteReader())
   {
      while (reader.Read())
      {
         Console.WriteLine(reader.GetTimeSpan(3));
      }
   }
   conn.Close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

A cast may be needed (SqlDataReader)reader the GetTimeSpan is not available in System.Data.IDataReader
0

Out of the top of my head, you might need to use something like this (haven't tested):

    public static void something()
    {
        string stmt = "select * from GazeTable where id = " + 1 + " ;";

        SqlConnection conn = GetConnection();
        SqlCommand cmd = new SqlCommand(stmt, conn);

        conn.Open();
        using (var reader = cmd.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0}", ((SqlDataReader)reader).GetTimeSpan(3).ToString(@"dd\.hh\:mm\:ss\:ff"));
                }
            }
            else
            {
                Console.WriteLine("No rows found.");
            }
        }
        conn.Close();
    }

You should convert the time(7) column to a timespan, you can't convert it directly to a datetime.

1 Comment

probably this will work too, but i will ensure that there is no Console.WriteLine("No rows found."); case. so thanks anyways
0

Note that if you are using the .NET Framework 4.8 or earlier GetTimeSpan() is not available. Instead you must use (TimeSpan)reader.GetValue(columnIndex)

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.