C# SQL查询中的减法时间

C# SQL查询中的减法时间,c#,sql,ms-access,C#,Sql,Ms Access,我在C应用程序中使用Access数据库。我需要一些关于在查询中减去时间的帮助。这是我的密码: DateTime TimePlus = DateTime.Now.AddMinutes(30); DateTime now = DateTime.Now; string Plus30Min = TimePlus.ToString("hh:mm tt"); string timeNow = now.ToString("hh:mm tt"); comma

我在C应用程序中使用Access数据库。我需要一些关于在查询中减去时间的帮助。这是我的密码:

     DateTime TimePlus = DateTime.Now.AddMinutes(30);
     DateTime now = DateTime.Now;

     string Plus30Min = TimePlus.ToString("hh:mm tt");
     string timeNow = now.ToString("hh:mm tt");

     command.Parameters.Add("tagNo", OleDbType.Integer).Value = tagNo;
     command.Parameters.Add("Plus30Min", OleDbType.VarChar).Value = Plus30Min;
     command.Parameters.Add("timeNow", OleDbType.VarChar).Value = timeNow;

               string query = @"SELECT s.TagID, se.SessionID, '" +
               DateTime.Now.ToString("MM/dd/yy HH:mm:ss tt") +
               "' AS ScanningTime " +
               " FROM (((Student s " +
               " LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
               " LEFT JOIN [ModuleID-SessionID] ms ON ms.ModuleID = cm.ModuleID) " +
               " LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
               " WHERE s.TagID = @tagNo " +
               " AND se.SessionDate = Date() " +
               " AND @timeNow >= DATE_SUB(SessionTimeStart,INTERVAL -30 MINUTE) " +
               " AND se.SessionTimeEnd >= @Plus30Min ";
时间是指数据库以下午18:00的格式保存。我可以在删除行后成功运行查询,这就是我遇到的问题

 " AND @timeNow >= DATE_SUB(SessionTimeStart,INTERVAL -30 MINUTE) " +
我的目标是用以下内容替换上述行:

"AND CurrentTime >= se.SessionTimeStart - 30 minutes "
我的完整C代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace AutoReg
{
    public partial class Form1 : Form
    {
        // Create the serial port with basic settings
        public SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        public int tagNo;

        public Form1()
        {
            InitializeComponent();

            //Attach a method to be called when there is data waiting in the port's buffer
            port.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceived);

            //Begin communications
            port.Open();
        }

        public void setSQL()
        {
            string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Kacper\\Desktop\\AutoReg\\AutoReg\\AutoReg.accdb;";

            //DateTime TimeMinus = DateTime.Now.AddMinutes(-30);
            DateTime TimePlus = DateTime.Now.AddMinutes(30);
            DateTime now = DateTime.Now;
            //string Minus30Min = TimeMinus.ToString("hh:mm tt");
            string Plus30Min = TimePlus.ToString("hh:mm tt");
            string timeNow = now.ToString("hh:mm tt");

            OleDbConnection MyConn = new OleDbConnection(ConnStr);
            MyConn.Open();

            DataSet ds = new DataSet();

            //SQL query that finds the current sessionID for the given tagID
            string query = @"SELECT s.TagID, se.SessionID, '" +
               DateTime.Now.ToString("MM/dd/yy HH:mm:ss tt") +
               "' AS ScanningTime " +
               " FROM (((Student s " +
               " LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
               " LEFT JOIN [ModuleID-SessionID] ms ON ms.ModuleID = cm.ModuleID) " +
               " LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
               " WHERE s.TagID = @tagNo " +
               " AND se.SessionDate = Date() " +
               " AND @timeNow >= DATEADD(MI,-30,se.SessionTimeStart) " +
               " AND se.SessionTimeEnd >= @Plus30Min ";

            OleDbCommand command = new OleDbCommand(query, MyConn);

            command.Parameters.Add("tagNo", OleDbType.Integer).Value = tagNo;
            //command.Parameters.Add("Minus30Min", OleDbType.VarChar).Value = Minus30Min;
            command.Parameters.Add("Plus30Min", OleDbType.VarChar).Value = Plus30Min;
            command.Parameters.Add("timeNow", OleDbType.DBTime).Value = timeNow;
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);

                adapter.Fill(ds);
                dataGridView3.DataSource = ds.Tables[0];
                MyConn.Close();

        }

        public void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (port.ReadChar() != 2) ;

            int v = 0;
            port.ReadChar(); // drop 1st 2 bytes - we actually only read the lower 32-bits of the code
            port.ReadChar();

            for (int i = 7; i >= 0; i--)
            {
                int c = port.ReadChar(); // a ascii hex char
                int part = c - '0';

                // test if 'Alpha'

                if (part > 9) part -= 7;     // Quick & dirty !


                v |= part << (i * 4);
            }

            for (int i = 0; i < 5; i++)
            {
                port.ReadChar();
            }

            tagNo = v; ;

            this.Invoke(new MethodInvoker(delegate()
            {

                //if the scanned tag already exists in the Student table...
                var foundIDs = autoRegDataSet.Student.Select("TagID = '" + tagNo + "'");
                if (foundIDs.Length != 0)
                //if (tagNo == autoRegDataSet.Student[0][0].ToString())
                {
                    textBox1.Text = "Tag in database, tagNO: " + v.ToString();
                    DataRow row = autoRegDataSet.Attendance.Rows.Add();
                    row[0] = v.ToString();
                    row[2] = DateTime.Now;
                }

                else
                {
                    textBox1.Text = "NOT in database, tagNO: " + v.ToString();
                }

            }));

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'autoRegDataSet.Attendance' table. You can move, or remove it, as needed.
            this.attendanceTableAdapter.Fill(this.autoRegDataSet.Attendance);
            // TODO: This line of code loads data into the 'autoRegDataSet.ActiveSessions' table. You can move, or remove it, as needed.
            this.activeSessionsTableAdapter.Fill(this.autoRegDataSet.ActiveSessions);
            // TODO: This line of code loads data into the 'autoRegDataSet.Student' table. You can move, or remove it, as needed.
            this.studentTableAdapter.Fill(this.autoRegDataSet.Student);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            setSQL();
        }
     }
}
使用DATEADD

CurrentTime >= DATEADD(MI, -30, se.SessionTimeStart)
您可以在MSDN上阅读有关DATEADD的更多信息

使用DATEADD

CurrentTime >= DATEADD(MI, -30, se.SessionTimeStart)
您可以在MSDN上阅读有关DATEADD的更多信息


和@timeNow>=DATEADDMI,-30,se.SessionTimeStart+现在我收到一个错误:System.Data.dll中发生类型为“System.Data.OleDb.OLEDBEException”的未处理异常。其他信息:没有为一个或多个必需参数提供值。您是否可以尝试将@timeNow从OleDbType.VarChar更改为OleDbType.DBTimepsst:您提供了T-SQL语法和链接。对于访问,语法应该是DateAddn,-30,[timeValue],链接是Oops。我的错。我没有看到连接字符串。感谢您指出Gord.Gord,感谢您的提示,我的程序现在不会生成任何错误,但同时也不会生成任何数据:/AND@timeNow>=DATEADDMI,-30,se.SessionTimeStart+现在我收到一个错误:System.Data.dll中发生类型为“System.Data.OleDb.OLEDBEException”的未处理异常。其他信息:没有为一个或多个必需参数提供值。您是否可以尝试将@timeNow从OleDbType.VarChar更改为OleDbType.DBTimepsst:您提供了T-SQL语法和链接。对于访问,语法应该是DateAddn,-30,[timeValue],链接是Oops。我的错。我没有看到连接字符串。感谢您指出Gord。Gord,感谢您的提示,我的程序现在不会生成任何错误,但同时也不会生成任何数据:/