Bonjour la communauté, je suis un débutant en C# et j'ai un petit problème d'insertion dans la BD.
J'ai utilisé D4M pour générer mes classes que j'ai essayé de modifier. Mais j'ai une erreur qui me casse la tête.
Je vous présente ci-dessous 3 bout de code:
- La classe connexion qui me permet de me connecter une BD Access
- La classe User qui me permet de gérer les utilisateurs
- L'appel de l'insertion d'un utilisateur(TbLogin et TbPwd étant des zones de texte)
La classe connexion
La classe User
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
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 using System; using System.Data; using System.Data.OleDb; using System.Collections; namespace D4M.DataBase { /// <summary> /// CnxBase /// </summary> public class CnxBase { private string _cnxString; public string cnxString { get { return _cnxString; } set { _cnxString = value; } } /// <summary> /// CnxBase /// </summary> public CnxBase() { cnxString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\AMIDON\BD ACHATS\ACHATS AMIDON.mdb;User ID=;Password=;"; } #region Méthodes de gestion /// <summary> /// renvoie une connexion SqlConnection ouverte /// </summary> public OleDbConnection ouvreConnexion(string connectionString) { try { OleDbConnection myOleDbConnection = new OleDbConnection(connectionString); myOleDbConnection.Open(); return myOleDbConnection; } catch(Exception myException) { throw(new Exception(myException.Message)); } } /// <summary> /// ferme la connexion SqlConnection reçue /// </summary> public void fermeConnexion(OleDbConnection myOleDbConnection) { try { if (myOleDbConnection.State == ConnectionState.Open) { myOleDbConnection.Close(); } } catch(Exception myException) { throw(new Exception(myException.Message)); } } /// <summary> /// renvoie un DataSet selon la requête donnée /// </summary> public DataSet obtenirDataSet(string strSQL) { CnxBase myBase = new CnxBase(); try { OleDbConnection myConn = myBase.ouvreConnexion(myBase.cnxString); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter (strSQL, myConn); DataSet myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet,"MySrcTable"); return myDataSet; } catch(Exception myException) { throw(new Exception(myException.Message +" => " + strSQL)); } } #endregion } }
l'appel (il s'agit de la méthode BtConnect_Click en fin de code)
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 using System; using System.Collections; using GesAmid; using System.Data; using System.Data.OleDb; //using System.Windows.Forms; namespace GesAmid { /// <summary> /// User /// </summary> public class User { #region ***** champs et propriétés ***** private string _Login; public string Login { get { return _Login; } set { _Login = value; } } private string _Pwd; public string Pwd { get { return _Pwd; } set { _Pwd = value; } } #endregion /// <summary> /// User /// </summary> public User() { } /// <summary> /// User /// </summary> public User(string Login,string Pwd) { this.Login = Login; this.Pwd = Pwd; } #region *****methodes de gestion pour la persistance /// <summary> /// ajoute un enregistrement /// </summary> /// <param name="myID"></param> public void ajouteUser(User myUser) { CnxBase myBase = new CnxBase(); string reqSQL = "INSERT INTO User (Login,Pwd) VALUES ('"+myUser.Login+"','"+myUser.Pwd+"')"; try { OleDbConnection myConn = myBase.ouvreConnexion(myBase.cnxString); OleDbCommand myCommand = new OleDbCommand(reqSQL,myConn); myCommand.ExecuteNonQuery(); myBase.fermeConnexion(myConn); } catch(Exception myErr) { throw(new Exception(myErr.ToString() + " " + reqSQL)); } } /// <summary> /// supprime l'enregistrement de la persistance selon l'identifiant donné /// </summary> /// <param name="myID"></param> public void effaceUser(int myID) { CnxBase myBase = new CnxBase(); try { OleDbConnection myConn = myBase.ouvreConnexion(myBase.cnxString); OleDbCommand myCommand = new OleDbCommand("DELETE FROM User WHERE (Login = "+myID+")",myConn); myCommand.ExecuteNonQuery(); myBase.fermeConnexion(myConn); } catch(Exception myErr) { throw(new Exception(myErr.ToString())); } } /// <summary> /// modifie un enregistrement /// </summary> public void modifieUser(User myUser) { CnxBase myBase = new CnxBase(); string reqSQL = "UPDATE User SET Login='"+myUser.Login+"',Pwd='"+myUser.Pwd+"' WHERE (Login="+myUser.Login+")"; try { OleDbConnection myConn = myBase.ouvreConnexion(myBase.cnxString); OleDbCommand myCommand = new OleDbCommand(reqSQL,myConn); myCommand.ExecuteNonQuery(); myBase.fermeConnexion(myConn); } catch(Exception myErr) { throw(new Exception(myErr.ToString() + reqSQL)); } } /// <summary> /// récupère une instance selon l'identifiant donné /// </summary> /// <param name="myID"></param> public User obtenirInstanceUser(string myID) { User myUser = new User(); CnxBase myBase = new CnxBase(); string reqSQL = "SELECT Login,Pwd FROM User WHERE (Login="+myID+")"; try { OleDbConnection myConn = myBase.ouvreConnexion(myBase.cnxString); OleDbCommand myCommand = new OleDbCommand(reqSQL,myConn); // MessageBox.Show("ServerVersion: " + myConn.ServerVersion // + "\nState: " + myConn.State.ToString() // + "\nautres: " +myConn.Provider); OleDbDataReader myReader = myCommand.ExecuteReader(); if(myReader.Read()) { myUser.Login = myReader[0].ToString(); myUser.Pwd = myReader[1].ToString(); } myBase.fermeConnexion(myConn); } catch(Exception myErr) { throw(new Exception(myErr.ToString() + reqSQL)); } return myUser; } /// <summary> /// renvoie un DataSet relatif aux enregistrements /// </summary> public DataSet obtenirTousUser() { CnxBase myBase = new CnxBase(); string reqSQL = "SELECT * FROM User"; try { CnxBase myD4MCnx = new CnxBase(); DataSet myResult = myD4MCnx.obtenirDataSet(reqSQL); return myResult; } catch(Exception myErr) { throw(new Exception(myErr.ToString() + reqSQL)); } } } #endregion }
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
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
140
141
142
143
144
145 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace GesAmid { /// <summary> /// Description résumée de FrmConnect. /// </summary> public class FrmConnect : System.Windows.Forms.Form { private System.Windows.Forms.Label LbLogin; private System.Windows.Forms.Label LbPwd; private System.Windows.Forms.TextBox TbPwd; private System.Windows.Forms.TextBox TbLogin; private System.Windows.Forms.Button BtConnect; /// <summary> /// Variable nécessaire au concepteur. /// </summary> private System.ComponentModel.Container components = null; public FrmConnect() { // // Requis pour la prise en charge du Concepteur Windows Forms // InitializeComponent(); // // TODO : ajoutez le code du constructeur après l'appel à InitializeComponent // } /// <summary> /// Nettoyage des ressources utilisées. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas /// le contenu de cette méthode avec l'éditeur de code. /// </summary> private void InitializeComponent() { this.LbLogin = new System.Windows.Forms.Label(); this.LbPwd = new System.Windows.Forms.Label(); this.TbLogin = new System.Windows.Forms.TextBox(); this.TbPwd = new System.Windows.Forms.TextBox(); this.BtConnect = new System.Windows.Forms.Button(); this.SuspendLayout(); // // LbLogin // this.LbLogin.AutoSize = true; this.LbLogin.Location = new System.Drawing.Point(16, 16); this.LbLogin.Name = "LbLogin"; this.LbLogin.Size = new System.Drawing.Size(32, 13); this.LbLogin.TabIndex = 0; this.LbLogin.Text = "Login"; // // LbPwd // this.LbPwd.AutoSize = true; this.LbPwd.Location = new System.Drawing.Point(16, 48); this.LbPwd.Name = "LbPwd"; this.LbPwd.Size = new System.Drawing.Size(72, 13); this.LbPwd.TabIndex = 1; this.LbPwd.Text = "Mot de passe"; // // TbLogin // this.TbLogin.Location = new System.Drawing.Point(104, 16); this.TbLogin.Name = "TbLogin"; this.TbLogin.Size = new System.Drawing.Size(96, 20); this.TbLogin.TabIndex = 2; this.TbLogin.Text = ""; // // TbPwd // this.TbPwd.Location = new System.Drawing.Point(104, 48); this.TbPwd.Name = "TbPwd"; this.TbPwd.PasswordChar = '+'; this.TbPwd.Size = new System.Drawing.Size(96, 20); this.TbPwd.TabIndex = 3; this.TbPwd.Text = ""; // // BtConnect // this.BtConnect.Location = new System.Drawing.Point(104, 80); this.BtConnect.Name = "BtConnect"; this.BtConnect.Size = new System.Drawing.Size(72, 24); this.BtConnect.TabIndex = 4; this.BtConnect.Text = "Connexion"; this.BtConnect.Click += new System.EventHandler(this.BtConnect_Click); // // FrmConnect // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 109); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.BtConnect, this.TbPwd, this.TbLogin, this.LbPwd, this.LbLogin}); this.Name = "FrmConnect"; this.Text = "FrmConnect"; this.ResumeLayout(false); } #endregion private void BtConnect_Click(object sender, System.EventArgs e) { User MyUser = new User(); MyUser.Login = this.TbLogin.Text; MyUser.Pwd = this.TbPwd.Text; MyUser.ajouteUser(MyUser); MessageBox.Show("Login : " + MyUser.Login + "\nPassWord: " + MyUser.Pwd); } } }
Lorsque je clique sur le bouton connect,j'ai l'erreur suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 "System.Data.OleDb.OleDbException: Erreur de syntaxe dans l'instruction INSERT INTO. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at GesAmid.User.ajouteUser(User myUser) in C:\Documents and Settings\MICROSOFT\Mes documents\Projets Visual Studio\GesAmid\User.cs:line 79"
et aussi l'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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 {"Erreur de syntaxe dans l'instruction INSERT INTO." } [System.Data.OleDb.OleDbException]: {System.Data.OleDb.OleDbException} System.Object: {System.Data.OleDb.OleDbException} _COMPlusExceptionCode: -532459699 _className: "System.Data.OleDb.OleDbException" _exceptionMethod: null _exceptionMethodString: null _message: "" _innerException: null _helpURL: null _stackTrace: {System.Array} _stackTraceString: " at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at GesAmid.User.ajouteUser(User myUser) in C:\Documents and Settings\MICROSOFT\Mes documents\Projets Visual Studio\GesAmid\User.cs:line 79" _remoteStackTraceString: null _remoteStackIndex: 0 _HResult: -2147217900 _source: null _xptrs: 0 _xcode: -532459699 Message: "Erreur de syntaxe dans l'instruction INSERT INTO." InnerException: null TargetSite: {System.Reflection.RuntimeMethodInfo} StackTrace: " at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at GesAmid.User.ajouteUser(User myUser) in C:\Documents and Settings\MICROSOFT\Mes documents\Projets Visual Studio\GesAmid\User.cs:line 79" HelpLink: "" Source: "Microsoft JET Database Engine" HResult: -2147217900
kelkun peut-il m'aider?
![]()
Partager