//All the values are split on ";" public static ScheduleFileModel FromCsv(string csvLine) { string[] values = csvLine.Split(';'); var schedulingValues = new ScheduleFileModel(); schedulingValues.Week = values[0]; schedulingValues.Day = values[1]; schedulingValues.Time = TimeSpan.Parse(values[2]); schedulingValues.ScheduleCode = Convert.ToInt32(values[3]); return(schedulingValues); }
//Contecting to database and insert the difrent values form the file in the map. private void Connect(FileInfo fI) { var connString = "Data Source=79.137.79.36;Initial Catalog=Powi;User ID=powi;Password=pinglan"; var sqlConnection = new SqlConnection(connString); List <ScheduleFileModel> values = File.ReadAllLines(_path + "\\" + fI.Name).Skip(1).Select(v => ScheduleFileModel.FromCsv(v)).ToList(); sqlConnection.Open(); using (var scope = new TransactionScope()) { string sqlIns = "INSERT INTO ScheduleFile (Week, Day, Time, ScheduleCode) VALUES(@week, @day, @time, @scheduleCode)"; foreach (var value in values) { var cmdIns = new SqlCommand(sqlIns, sqlConnection); cmdIns.Parameters.AddWithValue("@week", value.Week); cmdIns.Parameters.AddWithValue("@day", value.Day); cmdIns.Parameters.AddWithValue("@time", value.Time); cmdIns.Parameters.AddWithValue("@scheduleCode", value.ScheduleCode); cmdIns.ExecuteNonQuery(); } scope.Complete(); } sqlConnection.Close(); }