private void btnLoadData_Click(object sender, EventArgs e) { if (txtPath.Text != "") { string[] pathSplit = txtPath.Text.Split('\\'); int latestIndex = pathSplit.Length - 1; StreamReader sr; bool success = true; string fileName = pathSplit[latestIndex]; dbh.OpenConnectionToDB(); try { sr = new StreamReader(txtPath.Text); } catch (System.IO.DirectoryNotFoundException) { MessageHandler.ShowMessage(string.Format("Couldn't find the directory.")); success = false; } catch (System.IO.FileNotFoundException) { MessageHandler.ShowMessage("Couldn't find the file."); success = false; } catch (System.ArgumentException) { MessageHandler.ShowMessage("Unkown path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); success = false; } if (success) { sr = new StreamReader(txtPath.Text); string line = sr.ReadLine(); string[] value = line.Split(','); DataTable dt = new DataTable(); foreach (string dc in value) { dt.Columns.Add(new DataColumn(dc)); } while (!sr.EndOfStream) { value = sr.ReadLine().Split(','); if (value.Length == dt.Columns.Count) { DataRow row = dt.NewRow(); row.ItemArray = value; dt.Rows.Add(row); } else { MessageHandler.ShowMessage("Amount of columns not consistent"); return; } } SqlBulkCopy bc = new SqlBulkCopy(dbh.GetConnectionString(), SqlBulkCopyOptions.TableLock); if (!fileName.Contains("csv")) { MessageHandler.ShowMessage("This isn't a CSV file", "CSV Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (fileName.Contains("teams")) { bc.DestinationTableName = "TblTeams"; dbh.TruncateTable("TblTeams"); MessageHandler.ShowMessage("Teams toegevoegd"); } else if (fileName.Contains("matches")) { bc.DestinationTableName = "TblGames"; dbh.TruncateTable("TblGames"); MessageHandler.ShowMessage("Matches toegevoegd"); } else { MessageHandler.ShowMessage("There was no matching Table found for this csv file", "CSV Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } bc.BatchSize = dt.Rows.Count; bc.WriteToServer(dt); bc.Close(); } } dbh.CloseConnectionToDB(); }