public List<SupplierItem> DataTableToSupplierItems(DataTable dt) { List<SupplierItem> list = new List<SupplierItem>(); foreach (DataRow row in dt.Rows) { SupplierItem info = new SupplierItem(); info.Id = Convert.ToInt32(row["Id"]); info.SupplierId = Convert.ToInt32(row["SupplierId"]); info.ProductId = Convert.ToInt32(row["ProductId"]); info.Name = (string)row["Name"]; info.Price = Convert.ToDouble(row["Price"]); info.PriceDesc = (string)row["PriceDesc"]; info.Remark = (string)row["Remark"]; list.Add(info); } return list; }
public void InsertOrUpdateSupplierItem(SupplierItem item) { string InsSql = @"INSERT INTO Supplier_Item(ProductId,SupplierId,Name,Image,Price,PriceDesc,Remark,CreatedTime,ModifiedTime) " + " VALUES(@ProductId,@SupplierId,@Name,@Image,@Price,@PriceDesc,,@Remark,@CreatedTime,@ModifiedTime)"; string UpdSql = @"UPDATE Supplier_Item set ProductId=@ProductId,SupplierId=@SupplierId,Name=@Name,Image=@Image,Price=@Price, " + " PriceDesc=@PriceDesc,Remark=@Remark,ModifiedTime=@ModifiedTime WHERE Id = @Id"; string ExistRecordSql = "SELECT count(1) FROM Supplier_Item WHERE Id = " + item.Id; DateTime CurrentTime = DateTime.Now; MySqlParameter[] parameter = new MySqlParameter[] { new MySqlParameter("@Id",item.Id), new MySqlParameter("@ProductId",item.ProductId), new MySqlParameter("@SupplierId",item.SupplierId), new MySqlParameter("@Name",item.Name), new MySqlParameter("@Image",item.Image), new MySqlParameter("@Price",item.Price), new MySqlParameter("@PriceDesc",item.PriceDesc), new MySqlParameter("@Remark",item.Remark), new MySqlParameter("@CreatedTime", CurrentTime), new MySqlParameter("@ModifiedTime",CurrentTime) }; int record = Convert.ToInt32(dbHelper.ExecuteScalar(ExistRecordSql, null)); if (record == 0) { dbHelper.ExecuteNonQuery(InsSql, parameter); } else { dbHelper.ExecuteNonQuery(UpdSql, parameter); } }