private void InitializeApplicatoin() { try { if (RetryCount > 10) { MessageBox.Show("Applicatoin initialization failed"); } RetryCount++; if (!DBFileHelper.IsDBFileAvailable(AppDomain.CurrentDomain.BaseDirectory)) { MessageBox.Show("Please check DB file in app folder and it is not in read only mode"); Close(); } else if (!IsDBCompitable()) { MessageBox.Show("Current Database is not compitable, please update Database"); Close(); } else if (string.IsNullOrWhiteSpace(SharedRepo.UserRole)) { LoginForm loginForm = new LoginForm(); ShowFormAsFixedDialog(this, loginForm); } else { KeyValue keyValue = SharedRepo.DBRepo.GetKeyValue(SharedRepo.DBBackupDir); if (keyValue == null) { MessageBox.Show("DB backup directory is not configured. Please configure it "); } else { if (DBFileHelper.BackupDBFile(AppDomain.CurrentDomain.BaseDirectory, keyValue.Value)) { // If file backup is success then only delete old files DBFileHelper.DeleteOldBackupFiles(keyValue.Value); } else { MessageBox.Show("Taking DB backup failed to " + SharedRepo.DBRepo.GetKeyValue(SharedRepo.DBBackupDir).Value); } } // Check for Default Dealer/Customer. if (!(SharedRepo.DBRepo.DoesDefaulCustomerNameExist() && SharedRepo.DBRepo.DoesDefaulDealerNameExist())) { MessageBox.Show("Default dealer or default customer not found"); } } } catch (Exception ex) { MessageBox.Show("Something went wrong. \n " + ex.Message + "\n " + ex.InnerException.Message); } }
private void execute() { try { bool flag = false; string temp = tbComment.Text.Trim(); if (!string.IsNullOrEmpty(temp)) { DataRow[] drs = sc.dsTable.Tables[sc.dbName + sc.DBtable].Select("name = '" + tableInfo.TableName + "'"); if (drs != null && drs.Length > 0) { drs[0]["comments"] = temp; flag = true; DBFileHelper.WriteXml(sc.dsTable); } } foreach (ColumnInfo column in tableInfo.Columns) { if (column.IsChanged) { temp = column.Comments; DataRow[] drs = sc.dsTableColumn.Tables[sc.dbName + sc.DBtablesColumns].Select("TABLE_NAME = '" + tableInfo.TableName + "' and COLUMN_NAME = '" + column.Name + "'"); drs[0]["COMMENTS"] = temp; flag = true; DBFileHelper.WriteXml(sc.dsTableColumn); } } if (flag && cbExecuteDB.Checked) { DNCCFrameWork.DataAccess.IDbHelper db = new DNCCFrameWork.DataAccess.DbFactory( sc.dbConnectionString.Trim(new[] { '"' }), DBType.GetDbProviderString(sc.dbType)) .IDbHelper; string[] sql = createCommentSql().Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); foreach (var s in sql) { if (!string.IsNullOrEmpty(s)) { string t = s.Trim(new[] { '\t', '\r', '\n', ' ', ';' }); if (sc.OriginalEncoding != null && sc.TargetEncoding != null) { t = EncodingHelper. ConvertEncoder(sc.TargetEncoding, sc.OriginalEncoding, t); } if (!string.IsNullOrEmpty(t)) { db.ExecuteNonQuery(t); } } } } MessageBox.Show(this, "执行成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(this, "执行失败[" + ex.Message + "]", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void LoadDbInfoBySync(bool reloadDb) { _isLoadSuccess = false; DbConfigInfo dbConfigInfo = GetCurenctDbConfigInfo(); IniConfigHelper.WriteDefaultDBInfo(dbConfigInfo); string TargetEncoding = ""; string OriginalEncoding = ""; if (dbConfigInfo != null) { try { #region 贡献当前数据库配置信息 TargetEncoding = dbConfigInfo.DbEncoder; OriginalEncoding = EncodingHelper.OriginalEncoding; registerObject(PluginShareHelper.TargetEncoding, TargetEncoding); registerObject(PluginShareHelper.OriginalEncoding, OriginalEncoding); registerObject(PluginShareHelper.DBCurrentDBName, dbConfigInfo.DbConfigName); registerObject(PluginShareHelper.DBCurrentDBConnectionString, dbConfigInfo.ConnectionString.Trim('"')); registerObject(PluginShareHelper.DBCurrentDBType, dbConfigInfo.DbType); #endregion #region 加载所有表信息 SetStatusBar(string.Format("正在获取{0}中所有表信息", dbConfigInfo.DbConfigName)); SetProgress(0); if (_dsTable.Tables.Contains(dbConfigInfo.DbConfigName + Tables)) { _dsTable.Tables.Remove(dbConfigInfo.DbConfigName + Tables); } #region 从本地读取数据 bool status = DBFileHelper.IsExist(dbConfigInfo.DbConfigName, Tables); if (!reloadDb) { if (status) { DBFileHelper.ReadXml(_dsTable, dbConfigInfo.DbConfigName, Tables); } } #endregion #region 从数据库中读取数据 if (!status || reloadDb) { DNCCFrameWork.DataAccess.IDbHelper db = new DNCCFrameWork.DataAccess.DbFactory(dbConfigInfo.ConnectionString.Trim(new[] { '"' }), DBType.GetDbProviderString(dbConfigInfo.DbType)).IDbHelper; string sql = SqlDefHelper.GetTableNames(dbConfigInfo.DbType); DataSet temp = new DataSet(); db.Fill(sql, temp, new[] { dbConfigInfo.DbConfigName + Tables }); temp = DBFileHelper.WriteXml(temp, OriginalEncoding, TargetEncoding);//缓存表数据到本地 _dsTable.Merge(temp); } #endregion SetStatusBar(string.Format("{0}所有表信息加载完毕", dbConfigInfo.DbConfigName)); SetProgress(10); #endregion #region 加载所有表主键 SetStatusBar(string.Format("正在获取{0}中所有表主键信息", dbConfigInfo.DbConfigName)); if (_dsTablePrimaryKey.Tables.Contains(dbConfigInfo.DbConfigName + TablesPrimaryKeys)) { _dsTablePrimaryKey.Tables.Remove(dbConfigInfo.DbConfigName + TablesPrimaryKeys); } #region 从本地读取数据 status = DBFileHelper.IsExist(dbConfigInfo.DbConfigName, TablesPrimaryKeys); if (!reloadDb) { if (status) { DBFileHelper.ReadXml(_dsTablePrimaryKey, dbConfigInfo.DbConfigName, TablesPrimaryKeys); } } #endregion #region 从数据库中读取数据 if (!status || reloadDb) { DNCCFrameWork.DataAccess.IDbHelper db = new DNCCFrameWork.DataAccess.DbFactory(dbConfigInfo.ConnectionString.Trim(new[] { '"' }), DBType.GetDbProviderString(dbConfigInfo.DbType)).IDbHelper; string sql = SqlDefHelper.GetAllTablePrimaryKeys(dbConfigInfo.DbType); DataSet temp = new DataSet(); db.Fill(sql, temp, new[] { dbConfigInfo.DbConfigName + TablesPrimaryKeys }); temp = DBFileHelper.WriteXml(temp, OriginalEncoding, TargetEncoding);//缓存表数据到本地 _dsTablePrimaryKey.Merge(temp); } #endregion SetStatusBar(string.Format("{0}所有表主键信息加载完毕", dbConfigInfo.DbConfigName)); SetProgress(10); #endregion #region 构造树形结构 BindTreeBySync(); #endregion #region 加载所有表字段信息 SetStatusBar(string.Format("正在获取{0}中所有表字段信息", dbConfigInfo.DbConfigName)); if (_dsTableColumn.Tables.Contains(dbConfigInfo.DbConfigName + TablesColumns)) { _dsTableColumn.Tables.Remove(dbConfigInfo.DbConfigName + TablesColumns); } #region 从本地读取表字段信息 status = DBFileHelper.IsExist(dbConfigInfo.DbConfigName, TablesColumns); if (!reloadDb) { if (status) { DBFileHelper.ReadXml(_dsTableColumn, dbConfigInfo.DbConfigName, TablesColumns); SetProgress(80); } } #endregion #region 从数据库读取表字段信息 if (!status || reloadDb) { DNCCFrameWork.DataAccess.IDbHelper db = new DNCCFrameWork.DataAccess.DbFactory(dbConfigInfo.ConnectionString.Trim(new[] { '"' }), DBType.GetDbProviderString(dbConfigInfo.DbType)).IDbHelper; int count = _dsTable.Tables[dbConfigInfo.DbConfigName + Tables].Rows.Count; if (count != 0) { int temp1 = count / 7; bool isDivisible = count % temp1 == 0; string sql = SqlDefHelper.GetTableColumnNames(dbConfigInfo.DbType); DataSet dsTemp = new DataSet(); for (int i = 0; i < count; i++) { DataRow dr = _dsTable.Tables[dbConfigInfo.DbConfigName + Tables].Rows[i]; var temp = new DataSet(); var dicPar = new Dictionary <string, string> { { "@tableName", dr["name"] as string } }; SetStatusBar(string.Format("正在获取{0}中{1}字段信息", dbConfigInfo.DbConfigName, dr["name"] as string)); db.Fill(sql, temp, new[] { dbConfigInfo.DbConfigName + TablesColumns }, dicPar); dsTemp.Merge(temp); if (i % temp1 == 0) { SetProgress(10); } } dsTemp = DBFileHelper.WriteXml(dsTemp, OriginalEncoding, TargetEncoding); //缓存表字段数据到本地, _dsTableColumn.Merge(dsTemp); if (!isDivisible) { SetProgress(10); } } } #endregion SetEnable(true); #endregion _isLoadSuccess = true; } catch (Exception ex) { SetStatusBar(string.Format("加载数据失败[{0}]", ex.Message)); } } else { SetStatusBar("加载数据失败[没有获取到数据库配置]"); } }