Bonjour, j'ai un code en C# qui me donne du fil à retordre , je n'arrive pas à passer sur ma première fonction et à ainsi chercher les données dans ma base Access.

Le but de mon code est de chercher dans ma base les données que j'aurai tapé dans un module de saisie puis de voir si la personne saisie existe ou pas.


Je vous donne mon code pour que vous puissiez m'aider

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.IO;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Windows.Forms;
 
using Spigraph.SpiLib;
using Spigraph.Click2Data;
 
// Ne pas changer le nom de la classe ainsi que celui de la méthode statique principale
class SpiScript {
 
    const int NBR_MAX_INFOS_EXPDEST = 10;
    public enum TypSearch { Code, Nom }
 
	public int Main(ScriptsInterface c2d) 
	{
		try{
			MessageBox.Show("Here 1");
 
			switch( c2d.FieldName )
			{
 
				case "PB_SEARCH_CODE" :
					Search(TypSearch.Code, c2d.ControlGetValue("CodeClient"), c2d) ;
					break;
                		case "PB_SEARCH_NOM":
                    			Search(TypSearch.Nom, c2d.ControlGetValue("NomClient"), c2d);
                    			break;
                		case "LW_ClientList":
                    			OnSelectClient(c2d);
                    			break;
                		case "LW_RelationList":
                    			OnSelectRelation(c2d);
                    			break;
 
 
			}//switch()
 
			MessageBox.Show("Here Final");	
			return 0; //OK
		}
		catch(Exception ex)
		{
			System.Diagnostics.EventLog.WriteEntry( "CLICK2DATA", "Script OnClick - Exception : "+ex.Message, System.Diagnostics.EventLogEntryType.Error);
			return -1;
		}
	}//Main()
 
    /// <summary>
    /// Recherche multi-critere dans la BDD Excel en fonction des valeurs du formulaire
    /// </summary>
    private void Search(TypSearch a_typSearch, String a_value, ScriptsInterface c2d)
	{
	MessageBox.Show("Here Search1");
        a_value = a_value.ToUpper();
        //SpiLog.LogMessage(SpiMessages.InfoScript, "DEBUT Search(" + a_value +")", "Script_externe C#", "SpiScript.Main()"); //AVIRER
        DataControl lw = c2d.GetDataControl("LW_RelationList");
        lw.DeleteRows(); //RAZ
        lw = c2d.GetDataControl("LW_ClientList");
		lw.DeleteRows(); //RAZ
 
	MessageBox.Show("Here Search2");
 
 
		//Recherche dans la BDD
        String sPathDataBase_QGI = c2d.GetConfigParameter("IFT_QGI_PATH", "base-indexation-V2-02.mdb");
        //SpiLog.LogMessage(SpiMessages.InfoScript, "IFT_QGI_PATH=" + sPathDataBase_QGI + "=", "Script_externe C#", "SpiScript.Main()"); //AVIRER
        if (String.IsNullOrEmpty(sPathDataBase_QGI) || !File.Exists(sPathDataBase_QGI))
        {   //Erreur sur chemin
            SpiMisc.PlaySound(@"C:\windows\media\DING.wav");
            c2d.ShowErrorMessage("Parametre 'IFT_QGI_PATH' non défini ou invalide !");
            return;
        }
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + sPathDataBase_QGI;
 
        String sSelect = "SELECT Code, Nom, CodePostal, NomVille FROM Intervenants WHERE IsClient='1' AND ";
        if (a_typSearch == TypSearch.Code)
        {
            //sSelect += " WHERE Code = '{0}' ;";
            sSelect += " Code LIKE '{0}%' ;";
        }
        else
        {
            sSelect += " Nom LIKE '{0}%' ;";
        }
 
	MessageBox.Show("Here Connection");
 
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
		int iNbr = 0;
		using (DbConnection connection = factory.CreateConnection())
		{
			connection.ConnectionString = connectionString;
			using (DbCommand command = connection.CreateCommand())
			{
				command.CommandText = String.Format(sSelect, a_value) ;
                SpiLog.LogMessage(SpiMessages.WarningScript, "command.CommandText=" + command.CommandText + "=", "Script_externe C#", "SpiScript.Main()"); //AVIRER
 
				connection.Open();
				using (DbDataReader dr = command.ExecuteReader())
				{
					while (dr.Read())
					{	//Ajoute les éléments trouvés dans la ListView
                        string[] arRows = { dr["Nom"].ToString(), dr["CodePostal"].ToString(), dr["NomVille"].ToString() };
						lw.AddRow( dr["Code"].ToString(), arRows );
						iNbr++;
					}
				}
			}
		}
		if( iNbr < 1 )
		{	//Rien trouvé
			SpiMisc.PlaySound(@"C:\windows\media\Ding.wav");
            MessageBox.Show("Aucun client ne correspond au critère !", "Recherche");
 
		}
	}//Search()
 
 
    /// <summary>
    /// Actions réalisées sur la sélection d'une ligne de la ListView CLIENT
    /// </summary>
    private void OnSelectClient(ScriptsInterface c2d)
    {
        DataControl lwClient = c2d.GetDataControl("LW_ClientList");
        int iNbrRows = lwClient.GetRowCount();
        int iRowSelected = lwClient.GetRowSelected();
        String sTag = (String)lwClient.GetRowTag(iRowSelected >= 0 ? iRowSelected : iNbrRows - 1);
        //c2d.ShowInfoMessage( String.Format( "La ListView contient {0} lignes. La ligne {1} est sélectionnée. Son Tag a la valeur '{2}'",  iNbrRows, iRowSelected, sTag ) ); //DEBUG AVIRER
        //Positionne le Code et le Nom
        c2d.ControlSetValue("CodeClient", sTag);
        String[] arCols = (String[])lwClient.GetRowColumns(iRowSelected >= 0 ? iRowSelected : iNbrRows - 1);
        c2d.ControlSetValue("NomClient", arCols[0]); //RS
 
        //Mise à jour de la list RELATION
        DataControl lw = c2d.GetDataControl("LW_RelationList");
        lw.DeleteRows(); //RAZ
        //Acquisition des champs de la Relation
        String sPathDataBase_QTRS = c2d.GetConfigParameter("IFT_QTRS_PATH", "base-indexation-V2-02.mdb");
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + sPathDataBase_QTRS;
 
        String sSelect = String.Format("SELECT GUniqIDExp, GUniqIDDest FROM RelCED WHERE CodeClient='{0}' ;", sTag);
 
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        int iNbr = 0;
        using (DbConnection connection = factory.CreateConnection())
        {
            connection.ConnectionString = connectionString;
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = sSelect;
                //SpiLog.LogMessage(SpiMessages.InfoScript, "command.CommandText=" + command.CommandText + "=", "Script_externe C#", "SpiScript.Main()"); //AVIRER
 
                connection.Open();
                using (DbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {	//Ajoute les éléments trouvés dans la ListView
                        String sPath_QTRS = c2d.GetConfigParameter("IFT_QTRS_PATH", "base-indexation-V2-02.mdb");
                        String[] arExp = GetExpDest(sPath_QTRS, dr["GUniqIDExp"].ToString());
                        String[] arDest = GetExpDest(sPath_QTRS, dr["GUniqIDDest"].ToString());
                        String[] arRows = new String[8];
                        if (arExp != null && arDest != null)
                        {
                            arRows[0] = arExp[0]; arRows[1] = arExp[1]; arRows[2] = arExp[2]; arRows[3] = arExp[3];
                            arRows[4] =  arDest[0]; arRows[5] = arDest[1]; arRows[6] = arDest[2]; arRows[7] = arDest[3] ;
                        }
                        else
                        {
                            arRows[0] = dr["GUniqIDExp"].ToString(); arRows[1] = ""; arRows[2] = ""; arRows[3] = "";
                            arRows[4] = dr["GUniqIDExp"].ToString(); arRows[5] = ""; arRows[6] = ""; arRows[7] = "";
                        }
                        String sTmp = dr["GUniqIDExp"].ToString() + "|" + dr["GUniqIDDest"].ToString();
                        lw.AddRow(sTmp, arRows);
                        iNbr++;
                    }
                }
            }
        }
        if (iNbr < 1)
        {	//Rien trouvé
            SpiMisc.PlaySound(@"C:\windows\media\Ding.wav");
            MessageBox.Show("Aucun Expéditeur / Fournisseur ne correspond au critère !", "Recherche");
        }
        else
        {   //Ajout ligne blanche pour permettre RAZ des champs Expe et Dest
            String[] arRows = { "-", "-", " ", " ", "-", "-", " ", " " };
            lw.AddRow("|", arRows);
        }
 
        //c2d.ControlSetFocus("GUICHET");//SetFocus sur le champ suivant
 
    }//OnSelectClient()
 
 
 
 
    /// <summary>
    /// Actions réalisées sur la sélection d'une ligne de la ListView RELATION (RelCED + ExpDest)
    /// </summary>
    private void OnSelectRelation(ScriptsInterface c2d)
    {
        DataControl lw = c2d.GetDataControl("LW_RelationList");
        int iNbrRows = lw.GetRowCount();
        int iRowSelected = lw.GetRowSelected();
        String sTag = (String)lw.GetRowTag(iRowSelected >= 0 ? iRowSelected : iNbrRows - 1);
        //c2d.ShowInfoMessage(String.Format("La ListView contient {0} lignes. La ligne {1} est sélectionnée. Son Tag a la valeur '{2}'", iNbrRows, iRowSelected, sTag)); //DEBUG AVIRER
 
        String sPath_QTRS = c2d.GetConfigParameter("IFT_QTRS_PATH", "base-indexation-V2-02.mdb");
        String[] arTmp = sTag.Split("|".ToCharArray(), StringSplitOptions.None);
        String sUidExp = arTmp[0];
        String sUidDest = arTmp[1];
 
        //Positionne l'Expéditeur
        String[] arExp = GetExpDest(sPath_QTRS, sUidExp);
        if (arExp.Length > 0)
        {
            c2d.ControlSetValue("ExpGUniqID", sUidExp);
            c2d.ControlSetValue("ExpeNom", arExp[0]);
            c2d.ControlSetValue("ExpeCodePostal", arExp[1]);
            c2d.ControlSetValue("ExpeVille", arExp[2]);
            c2d.ControlSetValue("ExpePays", arExp[3]);
            c2d.ControlSetValue("ExpeNumVoie", arExp[4]);
            c2d.ControlSetValue("ExpeBTQ", arExp[5]);
            c2d.ControlSetValue("ExpeNomVoie", arExp[6]);
            c2d.ControlSetValue("ExpeComplement", arExp[7]);
            c2d.ControlSetValue("ExpeConsignes", arExp[8]);
        }
        else
        {
            c2d.ControlSetValue("ExpGUniqID", "");
            c2d.ControlSetValue("ExpeNom", "");
            c2d.ControlSetValue("ExpeCodePostal", "");
            c2d.ControlSetValue("ExpeVille", "");
            c2d.ControlSetValue("ExpePays", "");
            c2d.ControlSetValue("ExpeNumVoie", "");
            c2d.ControlSetValue("ExpeBTQ", "");
            c2d.ControlSetValue("ExpeNomVoie", "");
            c2d.ControlSetValue("ExpeComplement", "");
            c2d.ControlSetValue("ExpeConsignes", "");
        }
        //Positionne le Destinataire
        String[] arDest = GetExpDest(sPath_QTRS, sUidDest);
        if (arDest.Length > 0)
        {
            c2d.ControlSetValue("DestGUniqID", sUidDest);
            c2d.ControlSetValue("DestNom", arDest[0]);
            c2d.ControlSetValue("DestCodePostal", arDest[1]);
            c2d.ControlSetValue("DestVille", arDest[2]);
            c2d.ControlSetValue("DestPays", arDest[3]);
            c2d.ControlSetValue("DestNumVoie", arDest[4]);
            c2d.ControlSetValue("DestBTQ", arDest[5]);
            c2d.ControlSetValue("DestNomVoie", arDest[6]);
            c2d.ControlSetValue("DestComplement", arDest[7]);
            c2d.ControlSetValue("DestConsignes", arDest[8]);
        }
        else
        {
            c2d.ControlSetValue("DestGUniqID", "");
            c2d.ControlSetValue("DestNom", "");
            c2d.ControlSetValue("DestCodePostal", "");
            c2d.ControlSetValue("DestVille", "");
            c2d.ControlSetValue("DestPays", "");
            c2d.ControlSetValue("DestNumVoie", "");
            c2d.ControlSetValue("DestBTQ", "");
            c2d.ControlSetValue("DestNomVoie", "");
            c2d.ControlSetValue("DestComplement", "");
            c2d.ControlSetValue("DestConsignes", "");
        }
 
        c2d.ControlSetFocus("VoyagePoids");//SetFocus sur le champ suivant
 
    }//OnSelectRelation()
 
 
 
 
    private String[] GetExpDest(String a_Path_QTRS, String a_Uid)
    {
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + a_Path_QTRS;
 
        String[] arResultat = null;
 
        if (String.IsNullOrEmpty(a_Uid))
        {
            return new string[0];
        }
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        using (DbConnection connection = factory.CreateConnection())
        {
            connection.ConnectionString = connectionString;
            using (DbCommand command = connection.CreateCommand())
            {
 
                command.CommandText = String.Format("SELECT Nom, CodePostal, NomVille, CodePays, NumVoie, BTQ, NomVoie, Complement, ConsignesENL, ConsignesLIV FROM ExpDest WHERE GUniqID='{0}' ;", a_Uid);
                //SpiLog.LogMessage(SpiMessages.InfoScript, "command.CommandText=" + command.CommandText + "=", "Script_externe C#", "SpiScript.Main()"); //AVIRER
 
                connection.Open();
                using (DbDataReader dr = command.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        arResultat = new String[NBR_MAX_INFOS_EXPDEST];
                        arResultat[0] = dr["Nom"].ToString();
                        arResultat[1] = dr["CodePostal"].ToString();
                        arResultat[2] = dr["NomVille"].ToString();
                        arResultat[3] = dr["CodePays"].ToString();
                        arResultat[4] = dr["NumVoie"].ToString();
                        arResultat[5] = dr["BTQ"].ToString();
                        arResultat[6] = dr["NomVoie"].ToString();
                        arResultat[7] = dr["Complement"].ToString();
                        arResultat[8] = dr["ConsignesENL"].ToString() + "\r\n" + dr["ConsignesLIV"].ToString();
                    }
                }
            }
        }
        return arResultat;
    }
 
 
}//Class
Merci d'avance