Ciao a tutti, ho sviluppato un programma e funziona (mi sono sorpreso nell'aver fatto un programma che funziona). Il mio problema però è sorto quando ho impostato il framework di destinazione in 2.0 anzichè 4, perche non trova diversi riferimenti tra cui .Linq e la parte sull'Xdocument.
Nel pratico, questo mio programmino legge un file xml dove risiedono alcune impostazioni come connessione al db, cartella sorgente e cartella destinazione.
Crea l'elenco dei file nella cartella sorgente e per ogni file controlla se esiste un record in una tabella del db, se esiste sposta il file nella cartella destinazione ed esegue un INSERT in un'altra tabella.
Non cerco la soluzione pronta, ma mi accontento anche di qualche link dove trovare i riferimenti che mi servonocodice:using System; using System.IO; using System.Data.SqlClient; using System.Linq; using System.Xml.Linq; class Program { static void Main() { /* LETTURA FILE XML CON Le IMPOSTAZIONI */ string DataFile = @"Datafile.xml"; XDocument document = XDocument.Load(DataFile); string Connection = (string)document.Element("Root").Element("ConnectionString").Value; string Source = (string)document.Element("Root").Element("Path").Attribute("Source").Value; string Dest = (string)document.Element("Root").Element("Path").Attribute("Dest").Value; /* LETTURA FILE DIRECTORY SOURCE */ string[] arrayFile = Directory.GetFiles(Source); foreach (string nameFile in arrayFile) { /* OPERAZIONI SULLA STRINGA DEL NOME FILE SEPARO NUM.INV GIORNO MESE ANNO */ string FileName = nameFile.Replace(Source, ""); int startIndex = FileName.IndexOf("."); string NewNameFile = FileName.Substring(0, startIndex); string[] splitFile = NewNameFile.Split('-'); string InventoryNumber = splitFile[0]; string Day = splitFile[1]; string Mounth = splitFile[2]; string Year = splitFile[3]; SqlConnection conn = new SqlConnection(); conn.ConnectionString = @Connection; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandTimeout = 300; string sql = @"SELECT ......."; cmd.CommandText = sql; SqlDataReader reader; reader = cmd.ExecuteReader(); if (reader.HasRows == true) { while (reader.Read()) { /* ESEGUO INSERT PER OGNI RECORD TROVATO */ SqlConnection connInsert = new SqlConnection(); connInsert.ConnectionString = @Connection; connInsert.Open(); SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = connInsert; cmdInsert.CommandType = System.Data.CommandType.Text; cmdInsert.CommandTimeout = 300; string sqlInsert = @"INSERT INTO prova ([Inventario],[GUID],[Numero]) VALUES ('" + InventoryNumber + "','" + reader.GetString(0) + "','" + reader.GetInt32(1) +"')"; cmdInsert.CommandText = sqlInsert; SqlDataReader Insert; Insert = cmdInsert.ExecuteReader(); Insert.Close(); connInsert.Close(); } /* SPOSTO IL FILE NELLA NUOVA DIRECTORY */ File.Move(Source + FileName, Dest + FileName); Console.WriteLine("File: " + FileName + " Spostato"); } else { Console.WriteLine("Record non trovato"); } Console.ReadKey(); reader.Close(); conn.Close(); } } }
inoltre mi piacerebbe qualche consiglio su come migliorare il programma, non sono un programmatore professionista e molte cose sono dei copia-incolla, tipo ad esempio pensavo di creare una classe per la connessione al db, in modo da non dover riscrivere tutto ogni volta.
Grazie

 
			
			 
					
					
					
						 Rispondi quotando
  Rispondi quotando