求助大神vs窗体该怎么求sqlserver里两个时间相减的差。借阅系统。代码如下
问题描述:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=LAPTOP-C0MIMKL4\\SQLEXPRESSS;Initial Catalog=借阅系统;Integrated Security=True");
conn.Open();
string sql = "update book set benshu=benshu+'" + textBox3.Text + "' where bnum = '" + textBox1.Text.Trim() + "'";
string jtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string sql1 = "insert into borrow values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + "归还" + "','" + jtime + "')";
string sqllr = sql + sql1;
SqlCommand cmd = new SqlCommand(sqllr, conn);
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count > 0)
{
DateTime t1 = DateTime.Parse(jtime);
string str1 = "select time from borrow where bnum ='" + textBox1.Text.Trim()+"'";
DateTime t2 = Convert.ToDateTime(str1);
TimeSpan t3 = t1 - t2;
double getDay = t3.TotalDays;
double a = getDay - 30;
if (a > 0)
{
MessageBox.Show("归还成功!您已超期");
MessageBox.Show("您已超期" + a + "天\n罚金为" + a + "* 1元");
}
else
MessageBox.Show("归还成功");
}
textBox4.Text = jtime;
}
答
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime t0 = new DateTime(2016, 1, 1);
DateTime t1 = DateTime.Now;
// 求时间差
TimeSpan ts = t1 - t0;
Console.WriteLine("时间差,以秒计, {0}", ts.TotalSeconds);
Console.WriteLine("时间差,以分钟计, {0}", ts.TotalMinutes);
}
}
}
答
sqlserver中有计算函数,datediff