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
|
using System.Data.SqlClient;
namespace WinTablesOrphelines
{
public partial class Form2 : Form
{
private DataSet ds;
private DataTable dtItems, dtGisements;
private SqlDataAdapter daItems ,daGisements;
private SqlCommandBuilder builderItems, builderGisements;
private string strCon = string.Empty;
private SqlConnection con = null;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
ds = new DataSet();
strCon = GetConnectionString();
con = new SqlConnection();
con.ConnectionString = strCon;
con.Open();
}
private void btnFillItemsbtnFillItems_Click(object sender, EventArgs e)
{
if (con == null) return;
if (dtItems != null) ds.Tables.Remove(dtItems);
dtItems=new DataTable("Items");
ds.Tables.Add(dtItems);
string strSelectItems = "SELECT * FROM Items ";
daItems = new SqlDataAdapter(strSelectItems, con);
builderItems = new SqlCommandBuilder();
builderItems.DataAdapter = daItems;
builderItems.GetUpdateCommand();
builderItems.GetInsertCommand();
builderItems.GetDeleteCommand();
daItems.Fill(ds, dtItems.TableName);
dgvItems.DataSource = dtItems;
}
private void btnFillGisements_Click(object sender, EventArgs e)
{
if (con == null) return;
if (dtGisements != null) ds.Tables.Remove(dtGisements);
dtGisements = new DataTable("Gisements");
ds.Tables.Add(dtGisements);
string strSelectGisements = "SELECT * FROM Gisements";
daGisements = new SqlDataAdapter(strSelectGisements, con);
builderGisements = new SqlCommandBuilder();
builderGisements.DataAdapter = daGisements;
builderGisements.GetUpdateCommand();
builderGisements.GetInsertCommand();
builderGisements.GetDeleteCommand();
daGisements.Fill(ds, dtGisements .TableName);
dgvGisements.DataSource = dtGisements ;
// colonne KEY specifié EXPLICITEMNT
DataColumn[] keys = new DataColumn[1];
DataColumn column;
column = dtGisements.Columns[0];// C'EST ICI QUE CA SE PASSE
keys[0] = column;
dtGisements.PrimaryKey = keys;//ET ASSIGNATION
}
private void btnSaveItems_Click(object sender, EventArgs e)
{
if (daItems != null)
daItems.Update(ds, dtItems.TableName);
}
static private string GetConnectionString()
{
return WinTablesOrphelines.Properties.Settings.Default.MaBdConnectionString;
}
private Random rnd = new Random();
private void btnSaveGisements_Click(object sender, EventArgs e)
{
if (con == null) return;
DataTable Items=ds.Tables["Items"];
DataTable Gisements=ds.Tables["Gisements"];
DataColumn col = GetPrimaryKeys(Gisements);
foreach (DataRow item in Items.Rows)
{
int numeroGis =(int) item["NumeroGis"];
DataRow gis = Gisements.Rows.Find(numeroGis);
if (gis ==null) // SI CLE n'existe pas l'ajouter à Gisements
{
DataRow dr =Gisements.NewRow();
dr[col.ColumnName]=numeroGis;
dr["Montant"] =rnd.Next(5000,6001);
dr["Anne"] =DateTime.Now.ToShortDateString();
Gisements.Rows.Add(dr);
}
};
if(daGisements !=null)
daGisements.Update(ds, dtGisements.TableName);
}
private DataColumn GetPrimaryKeys(DataTable table)
{
// Create the array for the columns.
DataColumn[] columns;
columns = table.PrimaryKey;
return columns[0];// 1 ère colonne
}
}
} |
Partager