应该使用哪种数据类型来节省SQL服务器的时间 [英] which datatype should be used to save time only in SQL server
本文介绍了应该使用哪种数据类型来节省SQL服务器的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个数据库来保存在线考试生成的详细信息。
这里我想节省考试时间的持续时间,我正在使用datetime数据类型,但它需要当前日期不需要。
...下面是我的桌子结构...
I have created a database to save details of online-exam generation.
Here i want to save duration of exam time and i am using datetime datatype for this, but it takes current date also which is not needed.
...below is my table structure...
create table Exams
(
slNo int primary key identity(1,1),
examID varchar(10) unique not null,
title varchar(500) not null,
examHours datetime not null,
totalQn integer not null,
totalMarks integer not null
);
用户输入三个文本框格式的时间格式分别为txtHH,txtmm和txtSS。
这里是代码隐藏页面...
The user inputs the time in three textboxes in format txtHH , txtmm , and txtSS respectively.
here is codebehind page...
SqlConnection con = GlobalConnect.FnConnection();
con.Open();
string exmTime = string.Format(txtHH.Text.ToString() + ":" + txtMM.Text.ToString() + ":" + txtSS.Text.ToString());
SqlCommand cmd = new SqlCommand("Insert into dbo.Exams(examID,title,examHours,totalQn,totalMarks) values(@examID,@title,@examHours,@totalQn,@totalMarks)");
cmd.Parameters.AddWithValue("@examID", lblExmId.Text);
cmd.Parameters.AddWithValue("@title", txtTitle.Text.Trim());
cmd.Parameters.AddWithValue("@examHours",Convert.ToDateTime( exmTime));
cmd.Parameters.AddWithValue("@totalQn", TxtNoOfQn.Text.Trim());
cmd.Parameters.AddWithValue("@totalMarks", TxtTotMarks.Text.Trim());
int effect = GlobalConnect.ExecuteNonQuery(cmd);
请建议我怎么做。
Please suggest me how to do this.
推荐答案
使用int以分钟为单位存储持续时间。这可以根据需要转换为小时和分钟。
Use int to store the duration in minutes. This you can convert to hours & min as per your need.
使用整数,并存储累计秒数:您可以很容易地将其转换为小时数和分钟数如果你需要,并且转换为设置数据库值是微不足道的代码。
但是,你需要修补该代码:如果用户输入了错误的字符在文本框中,或指定77分钟,您的网站将崩溃。务必检查您的输入!
Use an integer, and store the cumulative number of seconds instead: you can convert that to a number of hours and minutes pretty easily if you need to, and the conversion to set teh DB value is trivial code.
However, you need to patch up that code: if the user enters a wrong character in a text box, or specifies 77 minutes, your website will crash. Always check your inputs!
这篇关于应该使用哪种数据类型来节省SQL服务器的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文