我正在为我的大学项目进行自动注册。我使用 RFID 扫描仪扫描学生的 tagID,并使用它来查询我的数据库并在 DataGridView 中显示结果。
运行时流程为:
用户扫描标签(RFID_DataReceived 处理程序触发读取标签并执行
TagExistsQuery()
以检查标签是否存在于Student
DB 的表中)如果标签确实存在于数据库中,则执行
AttendanceQuery()
以将标签与特定讲座匹配并在 DGV 中显示结果如果标签在 DB 中不存在,则显示错误消息
我的问题是AttendanceQuery()
多次产生相同的记录(准确地说是 16 次),而它应该只有一个。这让我疯了好几天了。希望您能提供帮助。
我的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 RoomActiveSession : Form
{
// Create the serial port with basic settings
public SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
public int tagNo;
public RoomActiveSession()
{
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 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()
{
TagExistsQuery();
}
));
}
//SQL query that checks if the scanned tag already exists in the "Student" table
public void TagExistsQuery()
{
DataTable queryResult = new DataTable();
string ConnStr = "Data Source=DUZY;Initial Catalog=AutoRegSQL;Integrated Security=True";
SqlConnection MyConn = new SqlConnection(ConnStr);
MyConn.Open();
string query = @"SELECT TagID" +
" FROM Student " +
" WHERE TagID = @tagNo ";
SqlCommand command = new SqlCommand(query, MyConn);
command.Parameters.Add("tagNo", SqlDbType.Int).Value = tagNo;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(queryResult);
if (queryResult.Rows.Count == 0)
{
MessageBox.Show("Unable to match scanned tag with the Student database. Please contact help desk for assistance");
MyConn.Close();
}
else
{
MyConn.Close();
AttendanceQuery();
}
}
//SQL query that finds the current sessionID for the given tagID by comparing curent date/time with date/time saved in DB and display result in DGV
public void AttendanceQuery()
{
DataTable queryResult = new DataTable();
string ConnStr = "Data Source=DUZY;Initial Catalog=AutoRegSQL;Integrated Security=True";
DateTime TimePlus = DateTime.Now.AddMinutes(30);
string Plus30Min = TimePlus.ToString("hh:mm tt");
SqlConnection MyConn = new SqlConnection(ConnStr);
MyConn.Open();
string query = @"SELECT s.TagID, se.SessionID, '" +
DateTime.Now +
"' 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 = cast(getdate() as date) " +
" AND se.SessionTimeStart <= @Plus30Min " +
" AND se.SessionTimeEnd >= @Plus30Min ";
SqlCommand command = new SqlCommand(query, MyConn);
command.Parameters.Add("tagNo", SqlDbType.Int).Value = tagNo;
command.Parameters.Add("Plus30Min", SqlDbType.VarChar).Value = Plus30Min;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(queryResult);
if (queryResult.Rows.Count == 0)
{
MessageBox.Show("Unable to register student " + tagNo);
MyConn.Close();
}
else
{
SetDataSouce(queryResult);
MyConn.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void SetDataSouce(object source)
{
dataGridView1.DataSource = source;
}
}
}
编辑:
感谢您迄今为止的贡献:
在您的大力帮助下(感谢@user123 和其他人),我设法确认以下代码还在 SSMS 上产生了 16 条记录:
DECLARE @Plus30Min TIME, @tagNo INT
SET @Plus30Min = DATEADD(MINUTE,30,GETDATE())
SET @tagNo = 4820427
SELECT s.TagID, se.SessionID
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 = cast(getdate() as date)
AND se.SessionTimeStart <= @Plus30Min
AND se.SessionTimeEnd >= @Plus30Min
使用 INNER JOIN 而不是 LEFT JOIN 仍然会产生 16 而不是 1
编辑光盘:
问题解决了。注意到其中一张表中的多条记录。我要感谢大家的帮助、指点和时间花费。像我这样的初学者很幸运有 SO 和像你这样愿意提供帮助的用户。再次感谢你!
编辑 CD2:
事实证明,从CourseID-ModuleID
表中删除多条记录,只将产生的记录从 16 限制到 4,因此有进步,但并不完美。我尝试使用 INNER 而不是 LEFT JOIN 但结果保持不变。
编辑 CD3: 再次检查表格,以及一些假条目。你 R 仍然很棒,而我在编程方面仍然很糟糕 :)