private void import_Click(object sender, RoutedEventArgs e) { try { foreach (StackPanel item in data_group.Children) { string table_name = item.Children.OfType <TextBlock>().First().Name; string path = item.Children.OfType <TextBox>().First().Text; if (string.IsNullOrEmpty(path)) { continue; } DataTable data = new DataTable(); List <object> modellist = new List <object>(); object model = new object(); switch (table_name) { case "WELL_STATUS": model = new Well_statusModel(); break; case "OIL_WELL_C": model = new Oil_well_cModel(); break; case "OIL_WELL_MONTH": model = new Oilwell_monthModel(); break; case "WATER_WELL_MONTH": model = new Waterwell_monthModel(); break; case "FZJ_MONTH": model = new Fzj_monthModel(); break; case "XSPM_MONTH": model = new Xspm_monthModel(); break; } data = ConvertTxtToTable(path); if (data == null) { throw new Exception("文件为空"); } Type type = model.GetType(); foreach (DataRow dr in data.Rows) { var instance = type.Assembly.CreateInstance(type.FullName); for (int i = 0; i < dr.Table.Columns.Count; i++) { if (dr.Table.Columns[i].ColumnName.Equals("ID")) { continue; } type.GetProperty(dr.Table.Columns[i].ColumnName).SetValue(instance, dr[i]); } modellist.Add(instance); } switch (table_name) { case "WELL_STATUS": BLL.Well_status.BatchAdd(modellist, table_name); break; case "OIL_WELL_C": BLL.OilWellC.BatchAdd(modellist, table_name); break; case "OIL_WELL_MONTH": BLL.OilWellMonth.BatchAdd(modellist, table_name); break; case "WATER_WELL_MONTH": BLL.WaterWellMonth.BatchAdd(modellist, table_name); break; case "FZJ_MONTH": BLL.FzjMonth.BatchAdd(modellist, table_name); break; case "XSPM_MONTH": BLL.XspmMonth.BatchAdd(modellist, table_name); break; } } MessageBox.Show("导入成功!"); this.Close(); } catch (Exception ex) { MessageBox.Show("导入失败!原因:" + ex.Message); } }
/// <summary> /// access库导入数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnFileImport_Click(object sender, RoutedEventArgs e) { if (dt_static == null || dt_static.Rows.Count == 0) { return; } Dictionary <string, string> dic = new Dictionary <string, string>(); List <ComboBox> comboBoxeHeaders = GetChildObjects <ComboBox>(upload_data, "ComboBox"); comboBoxeHeaders.RemoveAt(0); List <object> modellist = new List <object>(); object model = new object(); for (int i = 0; i < upload_data.Columns.Count; i++) { DataGridTextColumn dgcol = upload_data.Columns[i] as DataGridTextColumn; Binding binding = dgcol.Binding as Binding; string path = binding.Path.Path; if (path == comboBoxeHeaders[i].Text) { continue; } dic.Add(path, comboBoxeHeaders[i].Text); } foreach (DataRow dr in dt_static.Rows) { switch (table_name) { case "WELL_STATUS": model = new Well_statusModel(); break; case "OIL_WELL_C": model = new Oil_well_cModel(); break; case "OIL_WELL_MONTH": model = new Oilwell_monthModel(); break; case "WATER_WELL_MONTH": model = new Waterwell_monthModel(); break; case "FZJ_MONTH": model = new Fzj_monthModel(); break; case "XSPM_MONTH": model = new Xspm_monthModel(); break; } Type type = model.GetType(); foreach (KeyValuePair <string, string> kv in dic) { string propertyName = GetPropertyName(kv.Value); string value = dr[kv.Key].ToString(); type.GetProperty(propertyName).SetValue(model, value); } modellist.Add(model); } int num = 0; try { this.loading.Visibility = Visibility.Visible; Task AddData = Task.Run(() => { switch (table_name) { case "WELL_STATUS": num = BLL.Well_status.BatchAdd(modellist, table_name); break; case "OIL_WELL_C": num = BLL.OilWellC.BatchAdd(modellist, table_name); break; case "OIL_WELL_MONTH": num = BLL.OilWellMonth.BatchAdd(modellist, table_name); break; case "WATER_WELL_MONTH": num = BLL.WaterWellMonth.BatchAdd(modellist, table_name); break; case "FZJ_MONTH": num = BLL.FzjMonth.BatchAdd(modellist, table_name); break; case "XSPM_MONTH": num = BLL.XspmMonth.BatchAdd(modellist, table_name); break; } }).ContinueWith(t => { this.Dispatcher.Invoke(() => { this.loading.Visibility = Visibility.Collapsed; }); }); await AddData; MessageBox.Show("导入成功!共导入" + num + "条记录"); DialogResult = true; } catch (Exception ex) { MessageBox.Show("导入失败!原因:" + ex.ToString()); DialogResult = false; } }