qq_31341445
qq_31341445
采纳率100%
2017-06-19 03:02

求助大神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;

    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • BloodyMandoo BloodyMandoo 4年前
    点赞 1 评论 复制链接分享
  • wodewojueding wodewojueding 4年前

    sqlserver中有计算函数,datediff

    点赞 1 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 4年前

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

    }

    点赞 1 评论 复制链接分享