/// <summary> /// 绑定RFID卡与商品 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnBindingRfid_Click(object sender, EventArgs e) { if (curCardId == null) { MessageBox.Show("请放卡!"); return; } if (dgvBindingItem.CurrentRow == null) { MessageBox.Show("请选择商品!"); } if (curCardId.Equals("")) { Random rand = new Random(); int temp = rand.Next(1, 10000); for (; USDataAccess.Select($"select * from [terminal] where [cardId] like '{temp}';").Rows.Count > 0;) { temp = rand.Next(1, 10000); } curCardId = temp.ToString(); } for (; !serialPort.ReadAllData().Contains("addr:0x0001 para:0x");) { serialPort.Write($"CM+WRITE -addr=0x01 -value=0x{curCardId}"); } USDataAccess.Insert($"insert into [terminal] values(0,'{curCardId}',{dgvBindingItem.CurrentRow.Cells[0].Value.ToString()});"); curCardId = null; }
/// <summary> /// 添加新的数据行到数据库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvAddItem_RowEnter(object sender, DataGridViewCellEventArgs e) { if (dgvAddItem.CurrentRow != null && dgvAddItem.CurrentRow.Index == rowCount && isAdd == true) { string values = ""; foreach (DataGridViewCell item in dgvAddItem.Rows[rowCount].Cells) { if (item.Value.Equals(System.DBNull.Value)) { MessageBox.Show("数据不完整!", "添加新行失败"); dgvAddItem.ClearSelection(); dgvAddItem.Rows[item.RowIndex].Selected = true; item.Selected = true; return; } if (item.ValueType.Equals(typeof(int))) { values += item.Value.ToString() + ','; } else { values += $"'{item.Value.ToString()}',"; } } try { USDataAccess.Insert($"insert into [commodity] values({values.Trim(',')});"); BeginInvoke(new MethodInvoker(() => DGVDataSource = USDataAccess.Select("select id as 商品代码, name as 商品名, price as 价格, manufacturer as 生产厂家, productiondate as 生产日期, validuntil as 有效期, count(commodityId) as 库存数 from[terminal] as t1 join[commodity] as t2 on t1.commodityId = t2.id group by commodityId;"))); isAdd = false; } catch (Exception) { throw; } } }