如何节省sql Time DataType的时间 [英] How can I save time to sql Time DataType

查看:61
本文介绍了如何节省sql Time DataType的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi。我是c#的初学者,我对这个时间类型有很多问题:



1:我如何转换hh:mm(以字符串格式从TextBox获取--->以日期时间格式准确地说hh:mm:ss

2:如何在DateTime格式中转换hh:mm:ss--- >到Sql DataType Time(7)

3.如何在Time(7)中转换hh:mm:ssDataType --->准确地说这个格式的数据时间hh:mm:ss

4.向用户显示时间如何将hh:mm:ss转换为hh:mm

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"

推荐答案

首先,将其转换为TimeSpan对象:

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



然后,通过正常方式通过参数传递给SQL:


Then, pass it through to SQL via a parameter in the normal way:

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();
        }
    }







我定义了一个字段,代表人们对公司的入口时间。我希望将这个时间保存为我的sql Database的简短样式hh:mm:ss。所以我选择了时间(7)sql中的数据类型和C#类中的TimeStamp类型。现在我需要知道如何在sql中将时间从TimeStamp转换为Time(7),反之亦然。





Timespan和Time是可以互换的:只需在您的INSERT或UPDATE命令中将TimeSpan作为参数传递,SQL就会将其理解为时间。

同样,只需通过SELECT读取SQL返回的时间,并将其投射到TimeSpan - 系统将负责它。因此,如果Id是INT,而TimeStamp是TIME(7):




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):

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:

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);
                }
            }
        }
    }


我不确定你的要求但是,可能用户无需输入时间。您可以在表单提交时立即从服务器获取日期时间。



点击链接,这可能有助于您以不同的格式转换日期时间。



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





< a href =http://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx> http://msdn.microsoft.com/en-us/ library / system.datetime.now(v = vs.110).aspx [ ^ ]
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[^]


这篇关于如何节省sql Time DataType的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆