Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi.I'm beginner in c# and I have a lot of problem with this Time type:

1:How I convert "hh:mm"(got from TextBox) in string format--->to exactly "hh:mm:ss" in DateTime Format
2:How I convert "hh:mm:ss" in DateTime format---> to Sql DataType Time(7)
3.How I convert "hh:mm:ss" in Time(7) DataType---> to Datatime in exactly this formt"hh:mm:ss"
4.to show Time to user How I conver "hh:mm:ss" to "hh:mm"
Posted
Updated 3-Feb-14 22:19pm
v4
Comments
Herman<T>.Instance 4-Feb-14 3:28am    
"saving time in SqlDataReader" ???
mit62 4-Feb-14 4:19am    
I improve question

First, convert it to a TimeSpan object:
C#
string timeFromYourTextBox = "14:37";
TimeSpan time = TimeSpan.Parse(timeFromYourTextBox);

Then, pass it through to SQL via a parameter in the normal way:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myTimeColumn) VALUES (@THETIME)", con))
        {
        com.Parameters.AddWithValue("@THETIME", time);
        com.ExecuteNonQuery();
        }
    }



I defined a field in which represent entrance time of people to company.I want save this time in a short style "hh:mm:ss" to my sql Database.so I choosed Time(7) Data type in sql and TimeStamp Type in C# class.now I need to know How Convert time from TimeStamp to Time(7) in sql and vice versa.


Timespan and Time are interchangeable: just pass a TimeSpan as a Parameter in your INSERT or UPDATE command, and SQL will understand it as a Time.
Similarly, just read the Time back from SQL via a SELECT, and cast it to a TimeSpan - the system will take care of it. So, if Id is INT, and TimeStamp is TIME(7):
C#
using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True"))
    {
    con.Open();
    for (int i = 0; i < 5; i++)
        {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable2 (Id, TimeStamp) VALUES (@ID, @TS)", con))
            {
            cmd.Parameters.AddWithValue("@ID", i);
            cmd.Parameters.AddWithValue("@TS", new TimeSpan(1, i, 30));
            cmd.ExecuteNonQuery();
            }
        }
    }

And:
C#
using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True"))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, TimeStamp FROM myTable2", con))
        {
        using (SqlDataReader read = cmd.ExecuteReader())
            {
            while (read.Read())
                {
                int id = (int)read["Id"];
                TimeSpan ts = (TimeSpan)read["TimeStamp"];
                Console.WriteLine("{0}:{1}", id, ts);
                }
            }
        }
    }
 
Share this answer
 
v2
Comments
mit62 4-Feb-14 8:01am    
thanks.in this way timecolumn in sql is TimeSpan DataType or Time(7)?
OriginalGriff 4-Feb-14 8:06am    
SQL doesn't have a TimeSpan - so it will be Time(7).
When you read it out from SQL, you put it back into another TimeSpan in your C# code.
mit62 6-Feb-14 10:47am    
thnaks, but how should I Convert sql Time(7) type Data to TimeStamp?
I want Save sqldatareader["TimeDatatype"] to a TimeStamp C#Type, in this format:{"hh:mm:ss}

OriginalGriff 6-Feb-14 11:02am    
There is no standard .NET TimeStamp class, so I'll need to know a little more about what you are trying to do.
mit62 6-Feb-14 11:12am    
I defined a field in which represent entrance time of people to company.I want save this time in a short style "hh:mm:ss" to my sql Database.so I choosed Time(7) Data type in sql and TimeStamp Type in C# class.now I need to know How Convert time from TimeStamp to Time(7) in sql and vice versa.
I'm not sure about your requirement but, probably there is no need for the user to enter the time. You can get the date time instantly from the server on form submission.

Follow the links, which might help you to convert the date time in different formats.

http://blog.sqlauthority.com/2012/11/21/sql-server-display-datetime-in-specific-format-sql-in-sixty-seconds-033-video/[^]


http://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900