Rebonjour à tous.
Je sais qu'il y a un sujet similaire en ce moment même mais c'est pas vraiment la même chose au niveau du code donc je poste un autre topic...
Donc, je souhaite, à l'aide de TextBox et d'un bouton, ajouter de nouvelles données dans une table de ma DB Access.
Voici mon code :
Le code de ma classe ConnectToDB au cas où...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 ConnectToDB connect = new ConnectToDB(); // Déclarations de variables utilisables sur tout le projet. public static string nomNotice = string.Empty; public static DateTime dateNotice; public static string nomReaNotice = string.Empty; public static string refNotice = string.Empty; public static int verNotice = 0; private void bt_add_notice_Click(object sender, EventArgs e) { if ((tb_nom_notice.Text == "") || (dtp_notice.Value == null) || (tb_nom_rea.Text == "") || (tb_version.Text == "")) { MessageBox.Show("Un champ est vide, veuillez le remplir."); } else { // Les variables reçoivent les données saisies. nomNotice = tb_nom_notice.Text; dateNotice = dtp_notice.Value; nomReaNotice = tb_nom_rea.Text; refNotice = tb_ref.Text; verNotice = Convert.ToInt32(tb_version.Text); connect.fctConnectDB(); OleDbCommand addNewNotice = new OleDbCommand("Insert into Fiche (NomFiche, DateFiche, NomReaFiche, RefFiche, VerFiche) values(@nomNotice, @dateNotice, @nomRea, @refNotice, @verNotice)"); addNewNotice.Connection = connect.ConnectionDB; // Création des paramètres de la requête. OleDbParameter paramNomNotice = addNewNotice.CreateParameter(); paramNomNotice.ParameterName = "@nomNotice"; paramNomNotice.OleDbType = OleDbType.VarChar; paramNomNotice.Direction = ParameterDirection.Input; paramNomNotice.Value = nomNotice; OleDbParameter paramDateNotice = addNewNotice.CreateParameter(); paramDateNotice.ParameterName = "@dateNotice"; paramDateNotice.OleDbType = OleDbType.Date; paramDateNotice.Direction = ParameterDirection.Input; paramDateNotice.Value = dateNotice; OleDbParameter paramNomRea = addNewNotice.CreateParameter(); paramNomRea.ParameterName = "@nomRea"; paramNomRea.OleDbType = OleDbType.VarChar; paramNomRea.Direction = ParameterDirection.Input; paramNomRea.Value = nomReaNotice; OleDbParameter paramRefNotice = addNewNotice.CreateParameter(); paramRefNotice.ParameterName = "@refNotice"; paramRefNotice.OleDbType = OleDbType.VarChar; paramRefNotice.Direction = ParameterDirection.Input; paramRefNotice.Value = refNotice; OleDbParameter paramVerNotice = addNewNotice.CreateParameter(); paramVerNotice.ParameterName = "@refNotice"; paramVerNotice.OleDbType = OleDbType.Integer; paramVerNotice.Direction = ParameterDirection.Input; paramVerNotice.Value = verNotice; addNewNotice.Parameters.Add(paramNomNotice); addNewNotice.Parameters.Add(paramDateNotice); addNewNotice.Parameters.Add(paramNomRea); addNewNotice.Parameters.Add(paramRefNotice); addNewNotice.Parameters.Add(paramVerNotice); connect.fctDisconnectDB(); } }
Le souci c'est que l'enregistrement n'est pas créé et je n'ai aucun message d'erreur...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 class ConnectToDB { public System.Data.OleDb.OleDbConnection ConnectionDB; // Fonction de connexion à la DB. public void fctConnectDB() { ConnectionDB = new OleDbConnection(); ConnectionDB.ConnectionString = "Provider=microsoft.jet.oledb.4.0;Data Source=NoticePoste_be.mdb"; ConnectionDB.Open(); } // Fonction de déconnexion à la DB. public void fctDisconnectDB() { ConnectionDB.Close(); ConnectionDB.Dispose(); ConnectionDB = null; } }
Je me demandais si ça n'avait pas un rapport avec le fait que ma table Fiche a également un champ IdFiche qui est en numéro auto mais j'ai essayé de rajouter un param @idNotice qui prends pour valeur 3 (parce que je n'ai que 2 enregistrements dans la table fiche pour l'instant) mais ça n'a rien changé...
Est-ce que quelqu'un pourrait m'aider SVP ?
Par avance, merci.
PS : skunkies si tu lis ce post, oui j'ai fait des requêtes paramétrées ! Y'a que les c*** qui changent pas d'avis
Partager