我已经对软件进行了编码,它工作正常,并且 RFID 设备在登录框中也能正常工作,一旦人们使用 RFID 设备密钥登录,它就会进入主页但是 RFID 连接仍然打开并且不会关闭,当有人将其他密钥放在 RFID 设备附近时, 又打开一个窗口并使用其他登录 ID 自动登录,如何阻止它?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
//using System.Diagnostics;
//using System.IO.Ports;
using System.Speech.Synthesis;
using Phidgets;
using Phidgets.Events;
namespace GYM_Management
{
public partial class Login : GYM_Management.Form1
{
RFID rfid1;
string lastRFIDTag;
Int32 TagCtr;
public Login()
{
InitializeComponent();
lastRFIDTag = "";
TagCtr = 0;
}
connection cc = new connection();
SpeechSynthesizer sp = new SpeechSynthesizer();
private void Login_Load(object sender, EventArgs e)
{
textBox4.Enabled = true;
rfid1 = new RFID();
rfid1.Attach += new AttachEventHandler(rfid_Attach);
rfid1.Detach += new DetachEventHandler(rfid_Detach);
rfid1.RFIDTag += new TagEventHandler(rfid_Tag);
rfid1.RFIDTagLost += new TagEventHandler(rfid_TagLost);
rfid1.open();
Text = cc.Title() + " ( Login )";
groupBox1.Visible = false;
groupBox2.Visible = true;
}
public void RFIDLogin()
{
if (textBox4.Text.Length == 10)
{
SqlCeConnection con = new SqlCeConnection(cc.connectDB());
con.Open();
SqlCeCommand cmd = new SqlCeCommand("delete from log", con);
cmd.ExecuteNonQuery();
SqlCeCommand cmd1 = new SqlCeCommand("select * from Login where RFID='" + textBox4.Text + "'", con);
cmd1.ExecuteNonQuery();
SqlCeDataReader c = cmd1.ExecuteReader();
if (c.Read() == true)
{
SqlCeCommand cmd2 = new SqlCeCommand("select typeid from Login where RFID='" + textBox4.Text + "'", con);
Int32 count = (Int32)cmd2.ExecuteScalar();
if (count == 1)
{
SqlCeCommand cmd3 = new SqlCeCommand("insert into log values ('" + 1 + "')", con);
cmd3.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
Menu shw = new Menu();
shw.Show();
textBox4.Enabled = false;
txtTag.Enabled = false;
this.Hide();
sp.SpeakAsync("Welcome To Canvey Island GYM");
}
else
if (count == 2)
{
SqlCeCommand cmd3 = new SqlCeCommand("insert into log values ('" + 2 + "')", con);
cmd3.ExecuteNonQuery();
Menu shw = new Menu();
shw.Show();
textBox4.Enabled = false;
txtTag.Enabled = false;
this.Hide();
sp.SpeakAsync("Welcome To Canvey Island GYM");
}
}
else
{
//MessageBox.Show("Login Failed");
sp.SpeakAsync("Login Failed");
}
}
}
public void ent()
{
try
{
SqlCeConnection con = new SqlCeConnection(cc.connectDB());
con.Open();
SqlCeCommand cmd = new SqlCeCommand("delete from log", con);
cmd.ExecuteNonQuery();
SqlCeCommand cmd1 = new SqlCeCommand("select * from Login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'", con);
cmd1.ExecuteNonQuery();
SqlCeDataReader c = cmd1.ExecuteReader();
if (c.Read() == true)
{
SqlCeCommand cmd2 = new SqlCeCommand("select typeid from Login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'", con);
Int32 count = (Int32)cmd2.ExecuteScalar();
if (count == 1)
{
SqlCeCommand cmd3 = new SqlCeCommand("insert into log values ('" + 1 + "')", con);
cmd3.ExecuteNonQuery();
}
else
if (count == 2)
{
SqlCeCommand cmd3 = new SqlCeCommand("insert into log values ('" + 2 + "')", con);
cmd3.ExecuteNonQuery();
}
sp.SpeakAsync("Welcome To Canvey Island GYM");
Menu shw = new Menu();
shw.Show();
textBox4.Enabled = false;
this.Hide();
}
else
{
sp.SpeakAsync("Login Failed");
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
ent();
}
//private void textBox2_KeyDown(object sender, KeyEventArgs e)
//{
// if (e.KeyCode == Keys.Enter)
// {
// ent();
// }
//}
private void button2_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox2.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
groupBox2.Visible = false;
groupBox1.Visible = true;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
RFIDLogin();
rfid1.close();
}
void rfid_Tag(object sender, TagEventArgs e)
{
textBox4.Text = e.Tag;
txtTag.Text = e.Tag;
lastRFIDTag = txtTag.Text;
rfid1.LED = true; // light on
}
void rfid_TagLost(object sender, TagEventArgs e)
{
txtTag.Text = "";
rfid1.LED = false; // light off
//write held Tag ID to listview
lbPrevRFIDTags.Items.Insert(0,
string.Format("Tag: {0} - {1}", ++TagCtr, lastRFIDTag));
}
void rfid_Detach(object sender, DetachEventArgs e)
{
lblAttached.Text = "Not Attached";
}
void rfid_Attach(object sender, AttachEventArgs e)
{
Phidgets.RFID phid = (Phidgets.RFID)sender;
lblAttached.Text = "Attached: " + phid.Name;
lblSerial.Text = " Serial: " + phid.SerialNumber;
lblVersion.Text = " Version: " + phid.Version;
}
private void cboxAntenna_CheckedChanged(object sender, EventArgs e)
{
rfid1.Antenna = cboxAntenna.Checked;
}
}
}