//----- Process Import File Text private void ProcessImportFileText(object arrControl) { StreamReader sReader; countInsert = 0; int process = 0; int countErro = 0; object totalRow; string line; string[] lineParts, countLines; char[] charSpits; try { //----- Add control process from ArrayList arr1 = (ArrayList)arrControl; Label lb_title1 = (Label)arr1[0]; Label pictureBox_title1 = (Label)arr1[1]; NeroBar progressBar11 = (NeroBar)arr1[2]; Button btn_Stop1 = (Button)arr1[3]; DataGridView gridview1 = (DataGridView)arr1[4]; TabControl tabControl1 = (TabControl)arr1[5]; RichTextBox richTextBox1 = (RichTextBox)arr1[6]; Button btn_xuat1 = (Button)arr1[7]; //----- update display control lb_title1.Update(); btn_Stop1.Update(); gridview1.Update(); //----- Get Total row countLines = File.ReadAllLines(connectionString); totalRow = countLines.Length; if (totalRow == null) { return; } //----- Get Data from Source progressBar11.MaxValue = int.Parse(totalRow.ToString()); progressBar11.MinValue = 0; sReader = new StreamReader(connectionString); charSpits = (new char[] { charSpit }); //charSpit.ToCharArray(); while ((line = sReader.ReadLine()) != null) { //----- Stop process if false if (hasProcess) { process++; lineParts = line.Split(charSpits); //----- Test value import dienthoai_new model = new dienthoai_new(); if (dict.FirstOrDefault(x => x.Key == "cbb_TelNumber").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_TelNumber").Value)) { model.didong = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_TelNumber").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_ten_khach_hang").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ten_khach_hang").Value)) { model.ten_khach_hang = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ten_khach_hang").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_dia_chi").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_dia_chi").Value)) { model.dia_chi = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_dia_chi").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_Ngay").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_Ngay").Value)) { model.ngay = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_Ngay").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_Thang").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_Thang").Value)) { model.thang = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_Thang").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_gioitinh").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_gioitinh").Value)) { model.gioi_tinh = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_gioitinh").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_namsinh").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_namsinh").Value)) { model.namsinh = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_namsinh").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_nganhang").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_nganhang").Value)) { model.ngan_hang = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_nganhang").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_cuoc").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_cuoc").Value)) { model.cuoc = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_cuoc").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_sim").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_sim").Value)) { model.sim = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_sim").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_tinh").Value != "") { if (lineParts.Count() >= Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_tinh").Value)) { model.tinh = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_tinh").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_tinhcuoc").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_tinhcuoc").Value)) { model.tinh_cuoc = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_tinhcuoc").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_ghichu").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ghichu").Value)) { model.ghi_chu = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ghichu").Value)].Trim(); } } /*---------------------------------------------*/ if (dict.FirstOrDefault(x => x.Key == "cbb_phuong").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_phuong").Value)) { model.phuong = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_phuong").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_quanhuyen").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_quanhuyen").Value)) { model.quan_huyen = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_quanhuyen").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_email").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_email").Value)) { model.email = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_email").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_ngay_kich_hoat").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ngay_kich_hoat").Value)) { model.ngay_kich_hoat = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_ngay_kich_hoat").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_goiCuoc").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_goiCuoc").Value)) { model.goi_cuoc = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_goiCuoc").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_dongmay").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_dongmay").Value)) { model.dong_may = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_dongmay").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_hedieuhanh").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_hedieuhanh").Value)) { model.he_dieu_hanh = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_hedieuhanh").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_chucvu").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_chucvu").Value)) { model.chuc_vu = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_chucvu").Value)].Trim(); } } if (dict.FirstOrDefault(x => x.Key == "cbb_congty").Value != "") { if (lineParts.Count() > Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_congty").Value)) { model.cong_ty = lineParts[Convert.ToInt32(dict.FirstOrDefault(x => x.Key == "cbb_congty").Value)].Trim(); } } model.filenguon = nguon; thongbaokiemtra kq = KiemTraSoLieu(model); if (kq.trangthai) { //----- Test exists in database int nloi = 0; //if(kiemtra) // nloi = TestTelNumberExists(model.didong); if (nloi == 0) { //----- Insert to TelNumberChange Table if (SQLDatabase.AddDienThoaiNEW(model) == true) { countInsert++; } else { tberror.Rows.Add(process, model.didong, "Sai định dạng."); countErro++; } } else { tberror.Rows.Add(process, model.didong, "Đã tồn tại"); countErro++; /*da ton tai*/ } } else /*loi*/ { tberror.Rows.Add(process, model.didong, kq.NoiDung); countErro++; } ShowMessage(ref richTextBox1, process, countErro, countInsert, int.Parse(totalRow.ToString())); tabControl1.TabPages[1].Text = string.Format("Error {0}", countErro); tabControl1.Update(); progressBar11.Value = process; progressBar11.Update(); Thread.Sleep(0); } } if (countErro == 0) { this.DialogResult = DialogResult.OK; this.Close(); } btn_Stop1.Text = "Đóng"; btn_xuat.Enabled = true; } catch (Exception ex) { //MessageBox.Show(ex.Message, "ProcessImportFileText", MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = DialogResult.OK; this.Close(); } }
public void ProcessImport(object arrControl) { OleDbDataReader reader = null; object totalRow; try { //----- Add control process from ArrayList arr1 = (ArrayList)arrControl; Label lblPhanTram = (Label)arr1[0]; NeroBar progressBar11 = (NeroBar)arr1[1]; Label lblmessage = (Label)arr1[2]; //----- update display control lblPhanTram.Update(); lblmessage.Update(); totalRow = TongrowsText; if (totalRow == null) { return; } //----- Get Data from Source string[] columnNames = new string[columnNamesList.Count()]; for (int i = 0; i < columnNames.Count(); i++) { columnNames[i] = columnNamesList[i]; } /***********text*****************/ OleDbConnection con = new OleDbConnection(connectionString); con.Open(); OleDbDataAdapter dap = new OleDbDataAdapter("select * from [mau.txt]", con); DataTable dt = new DataTable(); dt.TableName = "Data"; dap.Fill(dt); reader = SQLDatabase.ExcOleReaderDataSource(connectionString, tableName, columnNames); progressBar11.MaxValue = int.Parse(totalRow.ToString()); progressBar11.MinValue = 0; progressBar11.Update(); // Set up the bulk copy object. using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLDatabase.ConnectionString)) { //http://csharp-video-tutorials.blogspot.com/2014/09/part-20-sqlbulkcopy-notifyafter-example_27.html bulkCopy.NotifyAfter = 5000; bulkCopy.BatchSize = 10000; bulkCopy.SqlRowsCopied += (sender, e) => { progressBar11.Value = e.RowsCopied; progressBar11.Update(); lblmessage.Text = string.Format("Insert số liệu vào database. ->{0}/{1}", e.RowsCopied.ToString("N0"), ConvertType.ToInt(totalRow).ToString("N0")); Thread.Sleep(0); }; bulkCopy.DestinationTableName = "dbo.dienthoai_new"; if (dict.FirstOrDefault(x => x.Key == "cbb_TelNumber").Value != "") { SqlBulkCopyColumnMapping cbb_TelNumber = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_TelNumber").Value, "didong"); bulkCopy.ColumnMappings.Add(cbb_TelNumber); } if (dict.FirstOrDefault(x => x.Key == "cbb_ten_khach_hang").Value != "") { SqlBulkCopyColumnMapping cbb_ten_khach_hang = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_ten_khach_hang").Value, "ten_khach_hang"); bulkCopy.ColumnMappings.Add(cbb_ten_khach_hang); } if (dict.FirstOrDefault(x => x.Key == "cbb_dia_chi").Value != "") { SqlBulkCopyColumnMapping cbb_dia_chi = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_dia_chi").Value, "dia_chi"); bulkCopy.ColumnMappings.Add(cbb_dia_chi); } if (dict.FirstOrDefault(x => x.Key == "cbb_Ngay").Value != "") { SqlBulkCopyColumnMapping cbb_Ngay = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_Ngay").Value, "ngay"); bulkCopy.ColumnMappings.Add(cbb_Ngay); } if (dict.FirstOrDefault(x => x.Key == "cbb_Thang").Value != "") { SqlBulkCopyColumnMapping cbb_Thang = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_Thang").Value, "thang"); bulkCopy.ColumnMappings.Add(cbb_Thang); } if (dict.FirstOrDefault(x => x.Key == "cbb_namsinh").Value != "") { SqlBulkCopyColumnMapping cbb_namsinh = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_namsinh").Value, "namsinh"); bulkCopy.ColumnMappings.Add(cbb_namsinh); } if (dict.FirstOrDefault(x => x.Key == "cbb_nganhang").Value != "") { SqlBulkCopyColumnMapping cbb_nganhang = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_nganhang").Value, "ngan_hang"); bulkCopy.ColumnMappings.Add(cbb_nganhang); } if (dict.FirstOrDefault(x => x.Key == "cbb_cuoc").Value != "") { SqlBulkCopyColumnMapping cbb_cuoc = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_cuoc").Value, "cuoc"); bulkCopy.ColumnMappings.Add(cbb_cuoc); } if (dict.FirstOrDefault(x => x.Key == "cbb_sim").Value != "") { SqlBulkCopyColumnMapping cbb_sim = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_sim").Value, "sim"); bulkCopy.ColumnMappings.Add(cbb_sim); } if (dict.FirstOrDefault(x => x.Key == "cbb_tinh").Value != "") { SqlBulkCopyColumnMapping cbb_tinh = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_tinh").Value, "tinh"); bulkCopy.ColumnMappings.Add(cbb_tinh); } if (dict.FirstOrDefault(x => x.Key == "cbb_ghichu").Value != "") { SqlBulkCopyColumnMapping cbb_ghichu = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_ghichu").Value, "ghi_chu"); bulkCopy.ColumnMappings.Add(cbb_ghichu); } if (dict.FirstOrDefault(x => x.Key == "cbb_gioitinh").Value != "") { SqlBulkCopyColumnMapping cbb_gioitinh = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_gioitinh").Value, "gioi_tinh"); bulkCopy.ColumnMappings.Add(cbb_gioitinh); } if (dict.FirstOrDefault(x => x.Key == "cbb_tinhcuoc").Value != "") { SqlBulkCopyColumnMapping cbb_tinhcuoc = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_tinhcuoc").Value, "tinh_cuoc"); bulkCopy.ColumnMappings.Add(cbb_tinhcuoc); } if (dict.FirstOrDefault(x => x.Key == "cbb_phuong").Value != "") { SqlBulkCopyColumnMapping cbb_Phuong = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_phuong").Value, "phuong"); bulkCopy.ColumnMappings.Add(cbb_Phuong); } if (dict.FirstOrDefault(x => x.Key == "cbb_quanhuyen").Value != "") { SqlBulkCopyColumnMapping cbb_quanhuyen = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_quanhuyen").Value, "quan_huyen"); bulkCopy.ColumnMappings.Add(cbb_quanhuyen); } if (dict.FirstOrDefault(x => x.Key == "cbb_email").Value != "") { SqlBulkCopyColumnMapping cbb_email = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_email").Value, "email"); bulkCopy.ColumnMappings.Add(cbb_email); } if (dict.FirstOrDefault(x => x.Key == "cbb_ngay_kich_hoat").Value != "") { SqlBulkCopyColumnMapping cbb_ngay_kich_hoat = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_ngay_kich_hoat").Value, "ngay_kich_hoat"); bulkCopy.ColumnMappings.Add(cbb_ngay_kich_hoat); } if (dict.FirstOrDefault(x => x.Key == "cbb_goiCuoc").Value != "") { SqlBulkCopyColumnMapping cbb_GoiCuoc = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_goiCuoc").Value, "goi_cuoc"); bulkCopy.ColumnMappings.Add(cbb_GoiCuoc); } if (dict.FirstOrDefault(x => x.Key == "cbb_dongmay").Value != "") { SqlBulkCopyColumnMapping cbb_dongmay = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_dongmay").Value, "dong_may"); bulkCopy.ColumnMappings.Add(cbb_dongmay); } if (dict.FirstOrDefault(x => x.Key == "cbb_hedieuhanh").Value != "") { SqlBulkCopyColumnMapping cbb_HeDieuhanh = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_hedieuhanh").Value, "he_dieu_hanh"); bulkCopy.ColumnMappings.Add(cbb_HeDieuhanh); } if (dict.FirstOrDefault(x => x.Key == "cbb_chucvu").Value != "") { SqlBulkCopyColumnMapping cbb_chucvu = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_chucvu").Value, "chuc_vu"); bulkCopy.ColumnMappings.Add(cbb_chucvu); } if (dict.FirstOrDefault(x => x.Key == "cbb_congty").Value != "") { SqlBulkCopyColumnMapping cbb_congty = new SqlBulkCopyColumnMapping(dict.FirstOrDefault(x => x.Key == "cbb_congty").Value, "cong_ty"); bulkCopy.ColumnMappings.Add(cbb_congty); } try { bulkCopy.WriteToServer(reader); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { progressBar11.Value = 100; progressBar11.Update(); lblmessage.Text = string.Format("Hoàn thành import {0}", int.Parse(totalRow.ToString())); Thread.Sleep(0); reader.Close(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "ProcessImport"); } }