0

我在 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 PM”格式保存的 DB。删除该行后我可以成功运行查询,这是我遇到的问题。

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

1 回答 1

3

使用日期添加

CurrentTime >= DATEADD(MI, -30, se.SessionTimeStart)

您可以在 MSDN 上阅读有关 DATEADD 的更多信息

http://msdn.microsoft.com/en-us/library/ms186819.aspx

于 2013-05-10T17:28:12.377 回答