Bonjour à tous,
J'ai un petit projet qui a pour but de transférer un gros nombres de données dans une base de données Mysql. Pour ce faire, j'utilise un Data Load Infile comme utilisé dans un projet précédent.
Cependant pour une raison inconnue le Data load infile dans ce projet ne veut pas fonctionner, j'ai pourtant passer au peigne fin le projet et l'erreur mais impossible de trouver une solution :/
Je reçois cette erreur : Fatal error encountered during command execution
Cette erreur et provoquer lorsque j'essaie d'exécuter la commande du Data Load Infile (ps : ma connexion mysql est correcte)
Voici le code qui permet de transformer mon DataTable en fichier .txt :
Voici le code qui lance la commande MySQL :
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 public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders) { if (includeHeaders) { IEnumerable<String> headerValues = sourceTable.Columns .OfType<DataColumn>() .Select(column => QuoteValue(column.ColumnName)); writer.WriteLine(String.Join("µ", headerValues)); } IEnumerable<String> items = null; int i = 1; foreach (DataRow row in sourceTable.Rows) { items = row.ItemArray.Select(o => QuoteValue(o?.ToString().Replace(',', '.').Replace("\r\n", " ") ?? String.Empty)); writer.WriteLine(String.Join("µ", items)); if (i == 3) break; i++; } writer.Flush(); } private static string QuoteValue(string value) { return Regex.Replace(String.Concat("\"", value.Replace("\"", "\"\""), "\""), @"\\", @"\\"); }
Voici un exemple de résultat dans mon fichier tempUsers.txt :
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 string tempFileCsv = Environment.CurrentDirectory + @"\UserData\tempUsers.txt"; try { using (StreamWriter writer = new StreamWriter(tempFileCsv)) { WriteCsv.WriteDataTable(dt, writer, false); } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } //Injection via sql try { string query = @"LOAD DATA LOCAL INFILE '" + tempFileCsv + "' IGNORE INTO TABLE users FIELDS TERMINATED BY 'µ' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' "; MySqlCommand commandCreate; commandCreate = new MySqlCommand(query, this.mysqlCo); commandCreate.ExecuteNonQuery(); } catch (MySqlException e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.WriteLine("Données chargées avec succès !"); //System.IO.File.Delete(tempFileCsv);
Voici la structure de ma table MYSQL :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 "1"µ"SYSTEM"µ"MCP133"µ"PARFILUX"µ"W:\\WINBOOKS\\DATA\\PARFILUX"µ"F"µ"" "2"µ"SYSTEM"µ"MCP133"µ"TEST"µ"W:\\WINBOOKS\\DATA\\TEST"µ"F"µ"" "3"µ"SYSTEM"µ"MCP133"µ"CJARDIN2"µ"W:\\WINBOOKS\\DATA\\CJARDIN2"µ"F"µ""
Merci d'avance
Partager