private void btnDelete_Click(object sender, EventArgs e) { int m = 0; bool flag = false; for (m = 0; m < dgvDataBase.Rows.Count; m++) { if (dgvDataBase.Rows[m].Selected) { string Id = dgvDataBase.Rows[m].Cells[0].Value.ToString(); DialogResult result = MessageBox.Show(@"是否删除?", @"提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); { if (result == DialogResult.Yes) { if (DataBaseNameDic.GetDataBaseNameDic().Delete(Convert.ToInt32(Id))) { bool deleResult = false; //同时删除与该数据库有关的配置 //删除配置表的信息 List <ConfigTable> listConfigTable = ConfigTableDic.GetConfigTableDic().SelectList(); for (int i = 0; i < listConfigTable.Count; i++) { if (listConfigTable[i].DataBaseId == Convert.ToInt32(Id)) { deleResult = ConfigTableDic.GetConfigTableDic().Delete(Convert.ToInt32(listConfigTable[i].ID)); } } //删除数据表的信息 List <TableFieldInfo> listTable = TableFieldInfoDic.GetTableFieldInfoDic().GetAllTableFieldInfos(); for (int i = 0; i < listTable.Count; i++) { if (listTable[i].DataBaseId == Convert.ToInt32(Id)) { deleResult = (deleResult && TableFieldInfoDic.GetTableFieldInfoDic().Delete(Convert.ToInt32(listTable[i].Id))); } } MessageBox.Show(@"删除成功"); flag = true; } else { MessageBox.Show(@"删除失败"); } } else { return; } } } } if (m == dgvDataBase.Rows.Count && !flag) { MessageBox.Show(@"请选择要删除的数据库信息"); m = 0; } dgvDataBase.Columns.Clear(); dgvDataBase.DataSource = null; DataBaseAddColumns(); dataGriewDataBaseSource = DataBaseNameDic.GetDataBaseNameDic().GetAllBaseNames(); dgvDataBase.DataSource = dataGriewDataBaseSource; dgvDataBase.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; if (dgvDataBase.Rows.Count > 0) { dgvDataBase.Rows[0].Selected = false; } BingDataSource(); }
//读取数据库中多有表的数据到内存中 public DataSet Select(DataBaseName database) { string connstr = Connectionstring.GetConnectionString(database); StringBuilder sqlstr = new StringBuilder(); List <ConfigTable> tables = ConfigTableDic.GetConfigTableDic().SelecConfigTables((int)database.ID); for (int i = 0; i < tables.Count; i++) { string str = string.Format( "select {0} as DataBaseID, '{1}' as TableName, ", (int)database.ID, tables[i].TableName); sqlstr.Append(str) .Append("[") .Append(tables[i].SensorId) .Append("],[") .Append(tables[i].ModuleNo); if (!string.IsNullOrEmpty(tables[i].ChannelId)) { sqlstr.Append("],[").Append(tables[i].ChannelId); } if (!string.IsNullOrEmpty(tables[i].OtherFlag)) { sqlstr.Append("],[").Append(tables[i].OtherFlag); } sqlstr.Append("] from ").Append(tables[i].TableName); if (i != tables.Count - 1) { sqlstr.Append(";"); } } switch (database.DataBaseType) { case (int)DataBaseType.SQLite: try { var dbHelper = new FreeSun.Common.DB.DbHelperSqLiteP(connstr); return(dbHelper.Query(sqlstr.ToString())); } catch (Exception ex) { _logger.ErrorFormat("{0} :{1}", ex.Message, sqlstr); } return(new DataSet()); case (int)DataBaseType.SQLServer: try { var sqlhelper = new DbHelperSQLP(connstr); return(sqlhelper.Query(sqlstr.ToString())); } catch (Exception ex) { _logger.ErrorFormat("{0} :{1}", ex.Message, sqlstr); } return(new DataSet()); case (int)DataBaseType.ACCESSOld: case (int)DataBaseType.ACCESSNew: var oledbhelper = new DbHelperOleDbP(connstr); string[] sqlstrings = sqlstr.ToString().Split(';'); DataSet[] ds = new DataSet[sqlstrings.Length]; for (int i = 0; i < sqlstrings.Length; i++) { try { ds[i] = oledbhelper.Query(sqlstrings[i]); } catch (Exception ex) { _logger.ErrorFormat("{0} :{1}", ex.Message, sqlstrings[i]); } } DataSet retSet = new DataSet(); for (int i = 0; i < sqlstrings.Length; i++) { retSet.Merge(ds[i]); } return(retSet); default: return(new DataSet()); } }
private void BingDataSource() { int width = 0;//定义一个局部变量,用于存储自动调整列宽以后整个DtaGridView的宽度 //数据库信息绑定 dgvDataBase.Columns.Clear(); dgvDataBase.DataSource = null; DataBaseAddColumns(); dataGriewDataBaseSource = DataBaseNameDic.GetDataBaseNameDic().GetAllBaseNames(); dgvDataBase.DataSource = dataGriewDataBaseSource; dgvDataBase.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //配置表信息绑定 gdvConfigTable.Columns.Clear(); gdvConfigTable.DataSource = null; ConfigTableAddColumns(); dataGriewConfigTableSource = ConfigTableDic.GetConfigTableDic().SelectList(); gdvConfigTable.DataSource = dataGriewConfigTableSource; gdvConfigTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; for (int i = 0; i < this.gdvConfigTable.Columns.Count; i++) //对于DataGridView的每一个列都调整 { this.gdvConfigTable.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells); //将每一列都调整为自动适应模式 width += this.gdvConfigTable.Columns[i].Width; //记录整个DataGridView的宽度 } if (width > this.gdvConfigTable.Size.Width) //判断调整后的宽度与原来设定的宽度的关系,如果是调整后的宽度大于原来设定的宽度,则将DataGridView的列自动调整模式设置为显示的列即可,如果是小于原来设定的宽度,将模式改为填充。 { this.gdvConfigTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; } else { this.gdvConfigTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; } //数据表信息配置 dgvDataTableInfo.Columns.Clear(); dgvDataTableInfo.DataSource = null; DataTablesAddColumns(); dataGriewDataTableSource = TableFieldInfoDic.GetTableFieldInfoDic().GetAllTableFieldInfos(); dgvDataTableInfo.DataSource = dataGriewDataTableSource; dgvDataTableInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; width = 0; for (int i = 0; i < this.dgvDataTableInfo.Columns.Count; i++) //对于DataGridView的每一个列都调整 { this.dgvDataTableInfo.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells); //将每一列都调整为自动适应模式 width += this.dgvDataTableInfo.Columns[i].Width; //记录整个DataGridView的宽度 } if (width > this.dgvDataTableInfo.Size.Width) //判断调整后的宽度与原来设定的宽度的关系,如果是调整后的宽度大于原来设定的宽度,则将DataGridView的列自动调整模式设置为显示的列即可,如果是小于原来设定的宽度,将模式改为填充。 { this.dgvDataTableInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; } else { this.dgvDataTableInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; } if (dgvDataBase.Rows.Count > 0) { dgvDataBase.Rows[0].Selected = false; } }
/// <summary> /// 提取其他数据库 /// </summary> /// <param name="dbName"></param> private void ExtractOtherOneDb(DataBaseName dbName) { var bll = new SelectOtherTablesBll(); DataSet ds = null; log.Debug("提取其他数据库"); try { ds = bll.Select(dbName); } catch (Exception ex) { log.Error(ex.Message); } if (ds != null) { log.Debug("提取成功"); foreach (DataTable dt in ds.Tables) { try { List <Data> list = new List <Data>(); if (this.messagesShowEventHandler != null) { var msg = new StringBuilder(); msg.Append("提取").Append(dt.TableName).Append(dt.Rows.Count).Append("条数据"); log.Info(msg.ToString()); this.messagesShowEventHandler( this, new MessagesShowEventArgs { MessageType = MsgType.Info, MessagesShow = msg.ToString() }); } if (dt != null && dt.Rows.Count > 0) { int index = 0; bool flag = false; bool ultraSetFlag = false;//@Modify20150107by yww int flagCount = 0; ConcurrentDictionary <int, ConfigTableInfo> dic = new ConcurrentDictionary <int, ConfigTableInfo>(); ConfigTableInfo config = new ConfigTableInfo(); if ( !string.IsNullOrEmpty( TableFieldInfoDic.GetTableFieldInfoDic() .GeTableFieldInfo((int)dbName.ID, dt.TableName) .OtherFlag)) { index = 1; } if (ConfigTableDic.GetConfigTableDic().SelectList().Count > 0) { ConfigTable configtable = ConfigTableDic.GetConfigTableDic().SelecConfigTable((int)dbName.ID); //ConfigTable configtable = ConfigTableDic.GetConfigTableDic().SelecConfigTable((int)dbName.ID, dt.TableName); if (configtable != null) { flag = true; flagCount = 1; } // 特殊处理(在有Sensor配置的时候直接拿SensorID做为模块号) if (dt.TableName == "MainStreeData") { ultraSetFlag = true; flag = false; flagCount = 1; } } else { flag = false; flagCount = 2; } foreach (DataRow row in dt.Rows) { try { var data = new Data { ProjectCode = Convert.ToInt16(row[0]), DataBaseId = Convert.ToInt32(row[1]), SafeTypeId = Convert.ToInt32(row[2]), ChannelId = 1 }; data.DataSet = new List <double>(); if (flag) { int sensorid = 0; bool identifyBySerialNo = false; string serialNo = ""; if (row[4] != DBNull.Value) { if (int.TryParse(row[4].ToString(), out sensorid)) { data.SensorId = sensorid; identifyBySerialNo = false; } else { serialNo = row[4].ToString(); identifyBySerialNo = true; } //data.SensorId = Convert.ToInt32(row[4]); if (ConfigInfoTable.ConfigtableInfoDictionary.ContainsKey(data.DataBaseId)) { dic = ConfigInfoTable.ConfigtableInfoDictionary[data.DataBaseId]; } else { log.Error("字典中数据库ID关键字不存在"); continue; } if (!identifyBySerialNo) { if (dic.ContainsKey(data.SensorId)) { config = dic[data.SensorId]; } else { log.Error("字典中传感器ID关键字不存在"); continue; } } else { config = (from s in dic.Values where StringComparer.OrdinalIgnoreCase.Compare(s.Otherflag, serialNo) == 0 select s).FirstOrDefault(); if (config == null) { log.Error("字典中传感器唯一标示符不存在"); continue; } } data.ChannelId = config.ChannelId; data.MoudleNo = config.MoudleNo; } else // 没有传感器ID { data.MoudleNo = "1"; } } else { data.ChannelId = 1; if (!ultraSetFlag) { if (row[5] != DBNull.Value) { data.ChannelId = Convert.ToInt32(row[5]); } } if (row[4] != DBNull.Value) { data.MoudleNo = row[4].ToString(); } } if (index == 1) { data.OFlag = Convert.ToInt32(row[6]); } // 采集时间转换问题,提供几种常见格式的时间转换 DateTime acqtime = Convert.ToDateTime(row[3].ToString().Trim()); //string timestr = ; //string[] timeformats = // { // "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss.fff", // "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.fff", "yyyyMMddHHmmss", // "yyyyMMddHHmmss.fff","yyyy-MM-dd h:mm:ss","yyyy-M-d h:mm:ss" // }; //timestr //bool isSuccess = DateTime.TryParseExact( // timestr, // timeformats, // CultureInfo.CurrentCulture, // DateTimeStyles.None, // out acqtime); //AssumeLocal //if (!isSuccess) //{ // try // { // acqtime = Convert.ToDateTime(timestr); // } // catch // { // log.Error("时间格式转换失败;" + timestr); // } //} data.CollectTime = acqtime; for (int i = 4 + flagCount + index; i < row.ItemArray.Length; i++) { double value; double.TryParse(row[i].ToString().Trim(), out value); data.DataSet.Add(value); } list.Add(data); } catch (Exception ex) { log.Error(ex.Message); } } string strtime = dt.Compute("Max(ACQUISITION_DATETIME)", Boolean.TrueString).ToString(); ExtractionConfigDic.GetExtractionConfigDic() .UpdateExtractionConfig( new ExtractionConfig { DataBaseId = (int)dbName.ID, TableName = dt.TableName, Acqtime = strtime }); } //添加到队列 foreach (Data data in list) { datasTobesent.Enqueue(data); } } catch (Exception ex) { log.Error(ex.Message); } } } else { log.Debug("DataSet==null"); } }