1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace TVP
{
public class Program
{
static void Main(string[] args)
{
List<DetailBonReception> dbr = new List<DetailBonReception>();
dbr.Add(new DetailBonReception
{
Pro_Design = "Abattant",
Pro_Qte = 100,
Pro_PUA = 1020,
Pro_PUV = 1400
});
dbr.Add(new DetailBonReception
{
Pro_Design = "Ampoule Economique",
Pro_Qte = 100,
Pro_PUA = 2000,
Pro_PUV = 3500
});
int retval =new Program().Bon_Reception_Insert(new Bon_Reception
{
Dep_design = "Bopaka",
Details = dbr,
Rec_Ref = "AXX100",
Rec_date = DateTime.Now,
Rec_UserID = "laurent"
});
Console.ReadLine();
}
public static void CreateParametersWith_DataTable<T>(SqlCommand cmd, List<T> table)
{
DataTable dtProduits = new DataTable("UT_Produit");
dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_design", DataType = typeof(string) });
dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_Qte", DataType = typeof(int) });
dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_PUA", DataType = typeof(decimal) });
dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_PUV", DataType = typeof(decimal) });
if (table is List<T>)
{
var t = table as List<DetailBonReception>;
foreach (DetailBonReception dbr in t)
{
dtProduits.Rows.Add(dbr);
}
}
SqlParameter param = cmd.Parameters.AddWithValue("@lstPrd", dtProduits);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.UT_Produit";
}
int Bon_Reception_Insert(Bon_Reception br)
{
using (SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("Bon_Reception_Insert", db))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter prmDep_Design = new SqlParameter("@dep_design", SqlDbType.VarChar, 50);
prmDep_Design.Value = br.Dep_design;
cmd.Parameters.Add(prmDep_Design);
//Reference de la reception
SqlParameter prmRec_Ref = new SqlParameter("@rec_ref", SqlDbType.VarChar, 5);
prmRec_Ref.Value = br.Rec_Ref;
cmd.Parameters.Add(prmRec_Ref);
//Date de réception
SqlParameter prmRec_Date = new SqlParameter("@rec_date", SqlDbType.VarChar, 10);
prmRec_Date.Value = br.Rec_date.ToShortDateString();
cmd.Parameters.Add(prmRec_Date);
//Reference de la reception
SqlParameter prmRec_UserID = new SqlParameter("@Rec_UserName", SqlDbType.VarChar, 256);
prmRec_UserID.Value = br.Rec_UserID;
cmd.Parameters.Add(prmRec_UserID);
CreateParametersWith_DataTable<DetailBonReception>(cmd, br.Details);
try
{
db.Open();
int retval = cmd.ExecuteNonQuery();
db.Close();
return retval;
}
catch
{
return -1;
}
finally
{
db.Close();
cmd.Parameters.Clear();
}
}
}
}
}
public partial class Bon_Reception
{
public long Rec_id { get; set; }
public string Rec_Ref { get; set; }
public DateTime Rec_date { get; set; }
public string Rec_UserID { get; set; }
public string Dep_design { get; set; }
}
public partial class Bon_Reception
{
public List<DetailBonReception> Details { get; set; }
}
public class DetailBonReception
{
public string Pro_Design { get; set; }
public int Pro_Qte { get; set; }
public decimal Pro_PUA { get; set; }
public decimal Pro_PUV { get; set; }
}
} |
Partager