public void ImportAllFilesOfFolder()//function declares methods to import// { try { SqlConnection con = new SqlConnection(@"--constr---"); string sourceDir = @"--- folderPath---"; var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv"); foreach (string currentFile in IcsvFile) { StreamReader sr = new StreamReader(currentFile); string line = sr.ReadLine(); string[] value = line.Split(','); DataTable dt = new DataTable(); DataRow row; foreach (string dc in value) { dt.Columns.Add(new DataColumn(dc)); } while (!sr.EndOfStream) { value = sr.ReadLine().Split(','); if (value.Length == dt.Columns.Count) { row = dt.NewRow(); row.ItemArray = value; dt.Rows.Add(row); } } string q = "ALTER TABLE DeviceDetails REBUILD WITH (IGNORE_DUP_KEY=ON)"; SqlCommand cm = new SqlCommand(q, con); con.Open(); cm.ExecuteNonQuery(); con.Close(); SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock); bc.DestinationTableName = "DeviceDetails"; bc.BatchSize = dt.Rows.Count; con.Open(); bc.WriteToServer(dt); bc.Close(); con.Close(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)