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?
reader.GetInt(2)?Time(7)does not convert toDateTimebutTimeSpanin C#, so tryreader.GetTimeSpan(3)instead