//查询单个(查ID) public int Select(ref M_Purchase_Body Obj, int ID, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"select ID,Material_ID,Purchase_ID,Quantity,UnitPrice,TotalPrice from {TableName} where ID = {ID}", conn); SqlDataReader sdr; try { conn.Open(); sdr = cmd.ExecuteReader(); while (sdr.Read()) { Obj.ID = (int)sdr["ID"]; Obj.Material_ID = (int)sdr["Material_ID"]; Obj.Purchase_ID = (int)sdr["Purchase_ID"]; Obj.Quantity = (int)sdr["Quantity"]; Obj.TotalPrice = (float)sdr["TotalPrice"]; Obj.UnitPrice = (float)sdr["UnitPrice"]; return 1; } return -1; } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
//更新单个(查ID) public int Update(M_Purchase_Body Obj, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"update {TableName} set Material_ID = @Material_ID, Purchase_ID = @Purchase_ID, Quantity = @Quantity, UnitPrice = @UnitPrice, TotalPrice = @TotalPrice where ID = @ID", conn); SqlParameter par = new SqlParameter("@Material_ID", SqlDbType.Int); par.Value = Obj.Material_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Purchase_ID", SqlDbType.Int); par.Value = Obj.Purchase_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Quantity", SqlDbType.Int); par.Value = Obj.Quantity; cmd.Parameters.Add(par); par = new SqlParameter("@UnitPrice", SqlDbType.Float); par.Value = Obj.UnitPrice; cmd.Parameters.Add(par); par = new SqlParameter("@TotalPrice", SqlDbType.Float); par.Value = Obj.TotalPrice; cmd.Parameters.Add(par); par = new SqlParameter("@ID", SqlDbType.Int); par.Value = Obj.ID; cmd.Parameters.Add(par); try { conn.Open(); if (cmd.ExecuteNonQuery() > 0) { return 1; } else { return -1; } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
//查询多个(查ID) public int Select(ref List<M_Purchase_Body> Obj, List<int> ID, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"select ID,Material_ID,Purchase_ID,Quantity,UnitPrice,TotalPrice from {TableName} where ID = @ID", conn); SqlDataReader sdr; SqlParameter par = new SqlParameter("@ID", SqlDbType.Int); cmd.Parameters.Add(par); int tmpOut = 0; try { conn.Open(); foreach (int i in ID) { cmd.Parameters["@ID"].Value = ID; sdr = cmd.ExecuteReader(); while (sdr.Read()) { M_Purchase_Body TmpObj = new M_Purchase_Body(); TmpObj.ID = (int)sdr["ID"]; TmpObj.Material_ID = (int)sdr["Material_ID"]; TmpObj.Purchase_ID = (int)sdr["Purchase_ID"]; TmpObj.Quantity = (int)sdr["Quantity"]; TmpObj.TotalPrice = (float)sdr["TotalPrice"]; TmpObj.UnitPrice = (float)sdr["UnitPrice"]; Obj.Add(TmpObj); tmpOut++; } sdr.Close(); } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } if (ID.Count() == tmpOut) { return 1; } else { return -1; } } }
//插入单个 public int Insert(ref M_Purchase_Body Obj, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"insert into {TableName} (Material_ID,Purchase_ID,Quantity,UnitPrice,TotalPrice) values (@Material_ID,@Purchase_ID,@Quantity,@UnitPrice,@TotalPrice);select @@IDENTITY as int", conn); SqlParameter par = new SqlParameter("@Material_ID", SqlDbType.Int); par.Value = Obj.Material_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Purchase_ID", SqlDbType.Int); par.Value = Obj.Purchase_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Quantity", SqlDbType.Int); par.Value = Obj.Quantity; cmd.Parameters.Add(par); par = new SqlParameter("@UnitPrice", SqlDbType.Float); par.Value = Obj.UnitPrice; cmd.Parameters.Add(par); par = new SqlParameter("@TotalPrice", SqlDbType.Float); par.Value = Obj.TotalPrice; cmd.Parameters.Add(par); try { conn.Open(); decimal d = (decimal)cmd.ExecuteScalar(); Obj.ID = (int)d; if (Obj.ID > 0) { return 1; } else { return -1; } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
//查询单个(查ID) public static int Select(ref M_Purchase_Body Obj, int ID, ref string ErrMsg) { I_Purchase_Body I = (D_Purchase_Body)SimpleFactory.CreateObject(DBType.Purchase_Body); return I.Select(ref Obj, ID, ref ErrMsg); }
//更新单个(查ID) public static int Update(M_Purchase_Body Obj, ref string ErrMsg) { I_Purchase_Body I = (D_Purchase_Body)SimpleFactory.CreateObject(DBType.Purchase_Body); return I.Update(Obj, ref ErrMsg); }