namespace Module1 { public partial class DeviceDetails : Form { //Timer initialises// System.Windows.Forms Timer t = new System.Windows.Forms Timer(); public DeviceDetails() { InitializeComponent(); // ImportAllFilesOfFolder(); } //Initialize data connection componants// SqlConnection con = new SqlConnection(----some conn string----); DataTable dt; SqlDataAdapter dataAdap; SqlCommandBuilder scmd; private void DeviceDetails_Load_1(object sender, EventArgs e) { this.deviceDetailsTableAdapter.Fill(this.hospitalProjectDataSet.DeviceDetails); t.Interval = 100000; // interval in millisecond t.Tick += new EventEventHandler(t_Tick); t.Enabled = true; t.Start(); } void t_Tick(object sender, EventArgs e) { GetData("select from DeviceDetails.csv"); ImportAllFilesOfFolder(); }
public void ImportAllFilesOfFolder() { try { SqlConnection con = new SqlConnection(----con string----); string sourceDir = @"---folder path to import from---"; 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); } } 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)