Bonjour,
j'ai un soucis dans mon programme. J'ai fias une application windowsform simple, qui prend un fichier en entrée et qui génère un fichier excel dans le dossier que l'on choisi.
J'utilise une base mysql, le problème est que pour les petits fichiers choisis ça marche, mais quand je prend un fichire reel qui fais par exemple 50000 lignes, il tourne puis ne fait plus rien.
Je souhaite tester une nouvelle méthode en m'inspirant d'un ancien programme que j'ai repris à l'époque en utilisant un dataset, mais je seche un peu :
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
 
 
    private void btn_start_ficExcel_Click(object sender, EventArgs e)
        {
            // Set Minimum to 1 represente la 1ere ligne
            mabarre.Visible = true;
            mabarre.Minimum = 1;
 
            string FileToRead = txtbox_entree.Text;
 
            /*
                RECUPERATION DES SIRENS
             */
 
            String[] lines2 = File.ReadAllLines(FileToRead);
            List<String> sirens = new List<String>();
            for (Int32 index = 2; index < lines2.Count(); index += 14)
            {
                if (lines2[index].Substring(0, 5) == "DT1DT")
                {
                    sirens.Add(lines2[index].Substring(6, 14));
 
                }
 
            }
            // Set Maximum  - correspond au nombre de ligne du fichier
            mabarre.Maximum = File.ReadAllLines(FileToRead).Length;
            // Set the initial value of the ProgressBar.
            mabarre.Value = 1;
            // Set the Step property - 1 chaque ligne lu
            //mabarre.Step = 1;
            /*
               Connexion à la base mysql
            */
            string server = "xxxxxxxxx";
            string database = "xxxxxx";
            string user = "xxxxxxx";
            string password = "xxxxxxxx";
            string port = "xxxxxxx";
 
            string connString = "Server=" + server + "; Port=" + port + ";Database=" + database + ";Uid=" + user + ";Pwd=" + password + ";SSL Mode=None;default command timeout=0";
            string marequete = "";
 
 
 
 
            conn = new MySqlConnection(connString);
            try
            {
                conn.Open();
            }
            catch (MySqlException erreur)
            {
                MessageBox.Show(erreur.ToString());
                conn.Close();
            }
 
 
            /*
           CREATION ET OUVERTURE DU FICHIER EXCEL
        */
            DateTime today = (DateTime.Now).AddMonths(-1);
            string d = Convert.ToString(today);
            string yyyy = d.Substring(6, 4);
            string mm = d.Substring(3, 2);
            string jour = d.Substring(0, 2);
            string heure = d.Substring(11, 2);
            string minute = d.Substring(14, 2);
            string seconde = d.Substring(17, 2);
 
            repexc = folderBrowserDialog2.SelectedPath + "\\mondossier" + yyyy + mm + jour + "_" + heure + minute + seconde + ".xlsx";
            Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            xla.Visible = false;
            Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
            //WorksheetCollection worksheets = xla.Worksheets;
            Worksheet ws = (Worksheet)xla.ActiveSheet;
            ws.Cells[1, 1] = "colonne 1";
            ws.Cells[1, 2] = "colonne 2"; 
            ws.Cells[1, 3] = "colonne 3";
			...............
			...............
			//je créé les différents entete
 
            UneCellule = ws.get_Range("A1", "M1");
            ws.Columns.Font.Name = "MS Sans Serif";
            ws.Columns.Font.Size = 12;
 
 
            string nomcol1 = "";
            string nomcol2 = "";
            string nomcol3 = "";
			........
			....... //je créé les différentes variables
 
 
            /*
                Parcours de la base de données et récupération du résultat
            */
 
			//ancienne methode
            /*for (int a = 2; a < sirens.Count; a++) //
            {
 
                marequete=" select t0.tesccpcod, "+
               "t0.col1, "+
               "t0.col2, " +
               "t0.col3, " +
               "t0.xxxx, " +
               "t0.xxxx, " +
               "t1.xxxx," +
               "t2.xxxx," +
               "t2.xxxx," +
               "t2.xxxx," +
                "t1.xxxx," +
                "t1.xxxxx," +
                "t1.xxxx " +
                "from matable0 t0 " +
                 "INNER join matable1 t1 on t1.xxxx = t0.xxx " +
                 "inner join matable2 t2 on t2.xxxx = t0.xxxx " +
                 "inner join matable3 t3 on t3.xxxx = t0.xxxx " +
                 "inner join matable4 t4 on t4.xxxx = t0.xxxx " +
                 "where t0.num ='" + sirens[a] + "'";
 
                MySqlCommand cmd = new MySqlCommand(marequete, conn);
                  monreader = cmd.ExecuteReader();
 
                if (monreader.Read())
                {
                    nomcol1 = monreader.GetString(0);
                    nomcol2 = monreader.GetInt64(1).ToString();
                    nomcol3 = monreader.GetString(2);
                   ........ //jusque là pas de soucis je recupère mon résultat
 
 
                    //je rempli mon tableau pour rempli le fichier excel
                    ws.Cells[a, 1] = nomcol1;
                    ws.Cells[a, 2] = nomcol2;
					...............
 
 
                }
                else
                {
                    ws.Cells[a, 2] = sirens[a];//si ko, dans mon fichier excel je ne met que l'identifiant
 
                }
                   mabarre.PerformStep(); //je rempli ma barre
                   monreader.Close();
 
            }//fin du for
                */
 
			//nouvelle methode
			for (int a = 2; a < sirens.Count; a++) //
            {
			  marequete=" select t0.tesccpcod, "+
               "t0.col1, "+
               "t0.col2, " +
               "t0.col3, " +
               "t0.xxxx, " +
               "t0.xxxx, " +
               "t1.xxxx," +
               "t2.xxxx," +
               "t2.xxxx," +
               "t2.xxxx," +
                "t1.xxxx," +
                "t1.xxxxx," +
                "t1.xxxx " +
                "from matable0 t0 " +
                 "INNER join matable1 t1 on t1.xxxx = t0.xxx " +
                 "inner join matable2 t2 on t2.xxxx = t0.xxxx " +
                 "inner join matable3 t3 on t3.xxxx = t0.xxxx " +
                 "inner join matable4 t4 on t4.xxxx = t0.xxxx " +
                 "where t0.num ='" + sirens[a] + "'";	
 
 
				  DataSet mondataset = rech_mysql(marequete, "nom", nbRows);
				 //ici je seche :(
 
 
 
			}
 
            ws.SaveAs(repexc, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xla.Quit();
 
        }
 
 
 
    }//fin méthode
 
	  private static DataSet rech_mysql(string requete, string dsName, int nbrows)
        {
            // Connexion ORACLE et récupération par requête
            DataSet ds = new DataSet(dsName);
            nbCols = 0;
            nbRows = 0;
            try
            {
                MySqlCommand SqlCmd = new MySqlCommand(requete, conn);
                //Conn.Open();
                monreader = SqlCmd.ExecuteReader();
                nbCols = monreader.FieldCount;
                string code = "";
                string siret = "";
 
 
                while (monreader.Read())
                {
                    code = monreader.GetString(0); //je tente de rempli mon dataset
                    siret = monreader.GetString(1);
                    // nbRows++;
                }
                monreader.Dispose();
                monreader.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("Impossible d'exécuter la requête !\r\n" + (String)ex.Message, "Request error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
 
            // Remplissage du dataset
            try 
            { 
                 MySqlCommand SqlCmd = new MySqlCommand(requete, conn);
                //Conn.Open();
                MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
                dataAdapter.SelectCommand = SqlCmd;
                dataAdapter.Fill(ds);
 
                SqlCmd.Dispose();
                SqlCmd = null;
                dataAdapter.Dispose();
                dataAdapter = null;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("Impossible d'exécuter la requête !\r\n" + (String)ex.Message, "Request error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            return ds;
 
        }
un grand merci pour votre aide.