RFID技術已經獲得了普遍應用。好比門禁系統、車輛入庫出庫都會有RFID讀寫設備在工做。其原理簡單講就是,讀寫器向本身的有效區域發射信號,RFID標籤會反射信號,讀寫器就讀到了標籤的信息。html
RFID標籤主要有4部分組成,分別是保留內存區、EPC、TID,和用戶自定義存儲區。其結構圖以下:sql
ref:四個區詳細內容介紹數據庫
咱們打算在JT-8290A讀寫器和配套通信協議基礎上基於Socket技術作一個Windows小程序。(只是讀EPC區,不涉及其餘區)小程序
開發工具:Visual Studio 2013c#
數據庫:5.5.43 MySQL Community Server (GPL)緩存
須要掌握的基礎:WPF概念,基本sql語句,後面可能用到多線程、泛型、Socket等。多線程
工做場景:RFID標籤貼到設備或物資上,物資進出的時候會被讀寫器掃描到,掃描記錄須要存儲,庫存記錄也須要存儲,在此基礎上設計出以下的數據庫。
工具
圖1-1 數據庫結構圖開發工具
People、type比較簡單。
測試
tags存儲標籤的EPC區信息和備註。
device表中typeName和tag字段是外鍵,repository表中tagID是外鍵,具體指向見圖1-1
設備進出歷史iohistory中tagID也是外鍵,這裏方便測試將這個外鍵刪掉了。。
項目源碼以及MysqlHelper.cs和MysqlData.dll等文件在(http://pan.baidu.com/s/1qWqKe5Y)的CSharp目錄下
數據庫增刪改查確定是要實現的,先實現「查」功能……
新建infoShow.xaml文件,在Canvas標籤下添加以下代碼
<Canvas Grid.Row="2"> <TabControl Canvas.Left="10" Canvas.Top="5" Height="297" Width="658"> <TabItem Header="全部標籤"> <Grid Background="AliceBlue"> <DataGrid AutoGenerateColumns="True" Height="Auto" Width="Auto" Margin="10,10,0,0" Name="dataGrid1" HorizontalAlignment="Left" VerticalAlignment="Top" IsReadOnly="True"/> </Grid> </TabItem> <TabItem Header="全部設備"></TabItem> <TabItem Header="全部人員"></TabItem> <TabItem Header="庫存記錄"></TabItem> <TabItem Header="進出歷史"></TabItem> <TabItem Header="類別查看"></TabItem> </TabControl> </Canvas>
對應的infoShow.xaml.cs
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; using System.Diagnostics; using System.Data; namespace Ex03_DeviceMS_wpf { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { string connectionString = "Database='databaseName';Data Source='localhost';User Id='uid';Password='password';charset='utf8';pooling=true"; public MainWindow() { InitializeComponent(); MySqlHelper.Conn = connectionString; this.GetData(); } private void GetData() { string cmd = "select * from tb_tags"; DataSet ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, cmd, null); this.dataGrid1.ItemsSource = ds.Tables[0].DefaultView; this.dataGrid1.AutoGenerateColumns = true;//自動 } } }
其餘TabItem下,顯示其餘表格的方法與之相似。
增刪改查功能(ref:wpf datagrid應用)
//添加以下Namespace using MySql.Data; using MySql.Data.MySqlClient; using System.Data; using System.Collections; using System.Configuration; namespace Ex02_wifiServer { /// <summary> ///MYSQLHelper 的摘要說明 /// </summary> public abstract class MySqlHelper { //數據庫鏈接字符串 public static string Conn { get; set; } // 用於緩存參數的HASH表 private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// <summary> /// 給定鏈接的數據庫用假設參數執行一個sql命令(不返回數據集) /// </summary> /// <param name="connectionString">一個有效的鏈接字符串</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>執行命令所影響的行數</returns> public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { MySqlCommand cmd = new MySqlCommand(); using (MySqlConnection conn = new MySqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 用現有的數據庫鏈接執行一個sql命令(不返回數據集) /// </summary> /// <param name="connection">一個現有的數據庫鏈接</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>執行命令所影響的行數</returns> public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> ///使用現有的SQL事務執行一個sql命令(不返回數據集) /// </summary> /// <remarks> ///舉例: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); /// </remarks> /// <param name="trans">一個現有的事務</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>執行命令所影響的行數</returns> public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> /// 用執行的數據庫鏈接執行一個返回數據集的sql命令 /// </summary> /// <remarks> /// 舉例: /// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); /// </remarks> /// <param name="connectionString">一個有效的鏈接字符串</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>包含結果的讀取器</returns> public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { //建立一個MySqlCommand對象 MySqlCommand cmd = new MySqlCommand(); //建立一個MySqlConnection對象 MySqlConnection conn = new MySqlConnection(connectionString); //在這裏咱們用一個try/catch結構執行sql文本命令/存儲過程,由於若是這個方法產生一個異常咱們要關閉鏈接,由於沒有讀取器存在, //所以commandBehaviour.CloseConnection 就不會執行 try { //調用 PrepareCommand 方法,對 MySqlCommand 對象設置參數 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); //調用 MySqlCommand 的 ExecuteReader 方法 MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); //清除參數 cmd.Parameters.Clear(); return reader; } catch { //關閉鏈接,拋出異常 conn.Close(); throw; } } /// <summary> /// 返回DataSet /// </summary> /// <param name="connectionString">一個有效的鏈接字符串</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns></returns> public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { //建立一個MySqlCommand對象 MySqlCommand cmd = new MySqlCommand(); //建立一個MySqlConnection對象 MySqlConnection conn = new MySqlConnection(connectionString); //在這裏咱們用一個try/catch結構執行sql文本命令/存儲過程,由於若是這個方法產生一個異常咱們要關閉鏈接,由於沒有讀取器存在, try { //調用 PrepareCommand 方法,對 MySqlCommand 對象設置參數 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); //調用 MySqlCommand 的 ExecuteReader 方法 MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = cmd; DataSet ds = new DataSet(); adapter.Fill(ds); //清除參數 cmd.Parameters.Clear(); conn.Close(); return ds; } catch (Exception e) { throw e; } } /// <summary> /// 用指定的數據庫鏈接字符串執行一個命令並返回一個數據集的第一列 /// </summary> /// <remarks> ///例如: /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); /// </remarks> ///<param name="connectionString">一個有效的鏈接字符串</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>用 Convert.To{Type}把類型轉換爲想要的 </returns> public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { MySqlCommand cmd = new MySqlCommand(); using (MySqlConnection connection = new MySqlConnection(connectionString)) { PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 用指定的數據庫鏈接執行一個命令並返回一個數據集的第一列 /// </summary> /// <remarks> /// 例如: /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); /// </remarks> /// <param name="connection">一個存在的數據庫鏈接</param> /// <param name="cmdType">命令類型(存儲過程, 文本, 等等)</param> /// <param name="cmdText">存儲過程名稱或者sql命令語句</param> /// <param name="commandParameters">執行命令所用參數的集合</param> /// <returns>用 Convert.To{Type}把類型轉換爲想要的 </returns> public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } /// <summary> /// 將參數集合添加到緩存 /// </summary> /// <param name="cacheKey">添加到緩存的變量</param> /// <param name="commandParameters">一個將要添加到緩存的sql參數集合</param> public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters) { parmCache[cacheKey] = commandParameters; } /// <summary> /// 找回緩存參數集合 /// </summary> /// <param name="cacheKey">用於找回參數的關鍵字</param> /// <returns>緩存的參數集合</returns> public static MySqlParameter[] GetCachedParameters(string cacheKey) { MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey]; if (cachedParms == null) return null; MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length]; for (int i = 0, j = cachedParms.Length; i < j; i++) clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms; } /// <summary> /// 準備執行一個命令 /// </summary> /// <param name="cmd">sql命令</param> /// <param name="conn">OleDb鏈接</param> /// <param name="trans">OleDb事務</param> /// <param name="cmdType">命令類型例如 存儲過程或者文本</param> /// <param name="cmdText">命令文本,例如:Select * from Products</param> /// <param name="cmdParms">執行命令的參數</param> private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null) { foreach (MySqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } } } }
[1] Karli Wastson. C#入門經典(第6版)[M].