我们在实现刷卡购物一系列的功能之前要先添加数据库,这里要求编程人员具有一定的数据库知识,增删改查这些用法一定要会。
另外,要做出该案例需要一些引用库,引用库如下
C#编程部分我们新建WPF项目,添加3个子窗口,Register子窗口样子示列:
实现该页面的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MWRDemoDll;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace Test15
{
/// <summary>
/// Register.xaml 的交互逻辑
/// </summary>
public partial class Register : Window
{
ResultMessage rm = new ResultMessage ();
SqlConnection conn = new SqlConnection ("server=Newland-PC; integrated security=true; database=SmartStore;");
Thread timer;
bool stop = true;
AutoResetEvent a = new AutoResetEvent (false);
public Register ()
{
InitializeComponent ();
timer = new Thread (Controls);
timer.IsBackground = true;
timer.Start ();
}
private void Controls ()
{
while (true)
{
if (stop)
a.WaitOne ();
MifareRFEYE.Instance.ConnDevice ();
rm = MifareRFEYE.Instance.Search ();
if (rm.Result != Result.Success)
{
MessageBox.Show (rm.OutInfo.ToString ());
}
else
{
Dispatcher.Invoke (() =>
{
txtbCard.Text = rm.Model.ToString ();
});
}
Thread.Sleep (1500);
}
}
private void btnReadCard_Click (object sender, RoutedEventArgs e)
{
stop = false;
a.Set ();
}
private void btnRegister_Click (object sender, RoutedEventArgs e)
{
try
{
string sql = string.Format ("insert into Register values ('{0}','{1}','{2}','{3}','{4}','{5}')", txtbCard.Text, txtAccount.Text, txtName.Text, txtPhone.Text, txtSex.Text, DateTime.Now.ToString ());
SqlCommand cmd = new SqlCommand (sql, conn);
if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Connecting)
{
conn.Close ();
}
else
{
conn.Open ();
}
cmd.ExecuteNonQuery ();
MessageBox.Show ("注册成功!");
}
catch
{
MessageBox.Show ("该用户已经存在!");
}
Close ();
}
private void btnCancel_Click (object sender, RoutedEventArgs e)
{
txtAccount.Text = "";
txtbCard.Text = "";
txtName.Text = "";
txtPhone.Text = "";
txtSex.Text = "";
Close ();
}
}
}
商品入库界面的实现:
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Srr1100U;
using System.ComponentModel;
using System.Threading;
using System.Data.SqlClient;
using System.Data;
namespace Test15
{
/// <summary>
/// Storage.xaml 的交互逻辑
/// </summary>
public partial class Storage : Window
{
SqlConnection conn = new SqlConnection ("server=Newland-PC; integrated security=true; database=SmartStore;");
SrrReader reader = new SrrReader ("COM3");
public Storage ()
{
InitializeComponent ();
}
private void btnCard_Click (object sender, RoutedEventArgs e)
{
reader.ConnDevice ();
reader.Read (Reader);
Thread.Sleep (1000);
reader.CloseDevice ();
}
private void Reader (string obj)
{
Dispatcher.Invoke (() =>
{
txtbCard.Text = obj;
});
}
private void btnSave_Click (object sender, RoutedEventArgs e)
{
try
{
if (txtbCard.Text == "" || txtName.Text == "" || txtNumber.Text == "" || txtprice.Text == "" || txtSpec.Text == "")
{
MessageBox.Show ("请补全信息!");
}
else
{
string sql = string.Format ("insert into commodity values('{0}','{1}','{2}','{3}','{4}')", txtName.Text, txtNumber.Text, txtprice.Text, txtSpec.Text, txtbCard.Text);
SqlCommand cmd = new SqlCommand (sql, conn);
if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Connecting)
{
conn.Close ();
}
else
{
conn.Open ();
}
cmd.ExecuteNonQuery ();
MessageBox.Show ("保存成功!");
}
}
catch
{
MessageBox.Show ("不能插入重复的商品!");
}
Close ();
}
protected override void OnClosing (CancelEventArgs e)
{
e.Cancel = true;
Hide ();
}
private void btnCancel_Click (object sender, RoutedEventArgs e)
{
txtbCard.Text = "";
txtName.Text = "";
txtNumber.Text = "";
txtprice.Text = "";
txtSpec.Text = "";
Close ();
}
}
}
商品结算页面的实现:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using MWRDemoDll;
using System.Data.SqlClient;
using NLE.Device.UHF;
using Srr1100U;
using System.Threading;
using System.ComponentModel;
namespace Test15
{
/// <summary>
/// Settlement.xaml 的交互逻辑
/// </summary>
public partial class Settlement : Window
{
PrintDialog dialog = new PrintDialog ();
ResultMessage rm;
SqlConnection conn = new SqlConnection ("server=Newland-PC; integrated security=true; database=SmartStore;");
UHFReader rfidDevice = new UHFReader ("COM6");
AutoResetEvent a = new AutoResetEvent (false);
bool stop = true;
int i=0, number;
double sum, s1;
Thread timer;
string Card,card2;
public Settlement ()
{
InitializeComponent ();
timer = new Thread (Readers);
timer.IsBackground = true;
timer.Start ();
}
private void Method_display ()
{
try
{
Dispatcher.Invoke (() =>
{
string sql1 = string.Format ("select c_number from commodity where c_Card=('{0}')", Card);
SqlCommand cmd1 = new SqlCommand (sql1, conn);
if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Connecting)
{
conn.Close ();
}
if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Connecting)
{
conn.Open ();
}
cmd1.ExecuteScalar ();
number = int.Parse (cmd1.ExecuteScalar ().ToString());
if (number >0)
{
number--;
string sql2 = string.Format ("update commodity set c_number='{0}' where c_Card=('{1}')", number, Card);
SqlCommand cmd2 = new SqlCommand (sql2, conn);
cmd2.ExecuteNonQuery ();
string sql = string.Format ("select c_Name from commodity where c_Card=('{0}')", Card);
SqlCommand cmd = new SqlCommand (sql, conn);
txtbName.Text += cmd.ExecuteScalar ().ToString ();
txtbnumber.Text += "X1" + "\n";
lblzong.Content = "总金额";
string sql3 = string.Format ("select c_Price from commodity where c_Card=('{0}')", Card);
SqlCommand cmd3 = new SqlCommand (sql3, conn);
txtprice.Text += "¥" + cmd3.ExecuteScalar ().ToString () + "\n";
s1=Convert.ToDouble( cmd3.ExecuteScalar ().ToString ());
sum += s1;
lblPrice.Content = sum;
Thread.Sleep (1000);
}
if(number<=0)
{
MessageBox.Show ("当前商品无货!");
}
});
}
catch
{
MessageBox.Show ("请放入RFID便签");
}
}
private void Readers ()
{
while (true)
{
if (stop)
a.WaitOne ();
Dispatcher.Invoke (() =>
{
if (rfidDevice != null)
{
string[] rfids = rfidDevice.ReadEpcSection ();
if (rfids.Length > 0)
{
foreach (var item in rfids)
Card = item;
}
}
});
MifareRFEYE.Instance.ConnDevice ();
rm = MifareRFEYE.Instance.Search ();
if (rm.Result != Result.Success)
{
MessageBox.Show (rm.OutInfo.ToString ());
}
else
{
Dispatcher.Invoke (() =>
{
card2 = rm.Model.ToString ();
Reader ();
});
}
Method_display ();
Thread.Sleep (3000);
}
}
private void Reader ()
{
Dispatcher.Invoke (() =>
{
string sql4 = string.Format ("select s_Name from Register where s_Card=('{0}')", card2);
SqlCommand cmd4 = new SqlCommand (sql4, conn);
if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Connecting)
{
conn.Close ();
}
if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Connecting)
{
conn.Open ();
}
lbltext.Content = cmd4.ExecuteScalar ().ToString () + "早上好,您购买的商品如下!";
});
}
private void btnCancel_Click (object sender, RoutedEventArgs e)
{
txtbName.Text = "";
txtbnumber.Text = "";
txtprice.Text = "";
lblPrice.Content = "";
lbltext.Content = "";
lblzong.Content = "";
Close ();
}
protected override void OnClosing (CancelEventArgs e)
{
e.Cancel = true;
Hide ();
}
private void Window_Loaded (object sender, RoutedEventArgs e)
{
stop = false;
a.Set ();
rfidDevice.Connect ();
}
private void btnjiesuan_Click (object sender, RoutedEventArgs e)
{
stop = true;
a.Reset();
if (dialog.ShowDialog() == true)
{
dialog.PrintVisual (Grid1, "Print Test");
}
}
}
}
主页面设计:
代码实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Test15
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Register register = new Register ();
Storage str = new Storage ();
Settlement sett = new Settlement ();
public MainWindow ()
{
InitializeComponent ();
}
private void btnRegister_Click (object sender, RoutedEventArgs e)
{
register.ShowDialog ();
}
private void btnStorage_Click (object sender, RoutedEventArgs e)
{
str.ShowDialog ();
}
private void btnSettlement_Click (object sender, RoutedEventArgs e)
{
sett.ShowDialog ();
}
}
}
本案例用到了部分设备,部分设备如图:
祝同学们学习越来越好,来自一个大二的小白。