示例#1
0
        public static DataTable RunSQLGetTable(string sql, DbTransformer ConnectionFacttory)
        {
            DataTable result = null;

            var dbConnection = ConnectionFacttory.MyDbConnection;

            try
            {
                dbConnection.Open();
                try
                {
                    var sqlCommand = dbConnection.CreateCommand();
                    sqlCommand.CommandText = sql;
                    IDataReader dr = sqlCommand.ExecuteReader();
                    result           = toDataTable(dr);
                    result.TableName = "dt";
                }
                catch (Exception)
                {
                    throw;
                }
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
            }
            catch (Exception)
            {
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
                throw;
            }
            return(result);
        }
        private void ModuleToDB(ibasModule module)
        {
            try
            {
                var args = new Core.ServiceEventArgs(string.Format("开始创建模块[{0}]的数据结构", module.ModuleDescription));
                this.MessageManager.OnWriteMessageLog(this, args);

                var dsGetter = new DataStructuresGetter();
                dsGetter.WorkFolder = module.ModuleInstallPath;
                var dsItems = dsGetter.Get();
                dsGetter.AutoSelected(this.B1Included, this.GetCurrentDBType(), dsItems);
                if (dsItems == null)
                {
                    return;
                }

                var logPath = string.Format(@"{0}Log\data_structures_{1}.txt", System.AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddhhmmss"));
                if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(logPath)))
                {
                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(logPath));
                }

                var gpDSItems = new Dictionary <string, List <DataStructureItem> >();
                foreach (var item in dsItems.Where(c => c.Selected))
                {
                    if (gpDSItems.ContainsKey(item.Group))
                    {
                        gpDSItems[item.Group].Add(item);
                    }
                    else
                    {
                        gpDSItems.Add(item.Group, new List <DataStructureItem>()
                        {
                            item
                        });
                    }
                }

                using (var logFile = System.IO.File.CreateText(logPath))
                {
                    logFile.WriteLine(string.Format("folder:{0}", this.AppSetting.InstallDiraddress));
                    foreach (var gpKey in gpDSItems.Keys)
                    {
                        List <string> files = null;
                        logFile.WriteLine(string.Format(" group:{0}", gpKey));
                        #region 数据结构创建
                        #region ibas
                        args = new Core.ServiceEventArgs(string.Format("正在创建模块[{0}]的ibas数据结构", module.ModuleDescription));
                        this.MessageManager.OnWriteMessageLog(this, args);

                        files = new List <string>();
                        foreach (var item in gpDSItems[gpKey].Where(c => c.ItemType == emDSItemType.data_structure &&
                                                                    c.Platform == emPlatform.ibas))
                        {
                            logFile.WriteLine(string.Format("  file:{0}", item.FilePath));
                            files.Add(item.FilePath);
                        }
                        if (files.Count > 0)
                        {
                            var xmlTrans  = new XmlTransformer();
                            var ibasModel = xmlTrans.ToDomainModel(files.ToArray());
                            if (xmlTrans.HasError)
                            {
                                throw xmlTrans.ErrorLogs.FirstOrDefault();
                            }

                            var dbTrans = new DbTransformer();
                            dbTrans.DBTypeSign = this.AppSetting.DatabaseType;
                            dbTrans.SetMySQLMap(new SQLMapFactory(this.AppSetting.DBServer, this.AppSetting.DBUser, this.AppSetting.DBPassword, this.AppSetting.DBName).GetSQLMap(dbTrans.DBTypeSign));
                            dbTrans.SetDB(new dbConnectionFactory(this.AppSetting.DBServer, this.AppSetting.DBUser, this.AppSetting.DBPassword, this.AppSetting.DBName).GetDBConnection(dbTrans.MySQLMap));
                            dbTrans.ToDB(ibasModel);

                            if (dbTrans.HasError)
                            {
                                throw new Exception(dbTrans.Errors.ToString());
                            }
                        }
                        #endregion
                        #region b1
                        args = new Core.ServiceEventArgs(string.Format("正在创建模块[{0}]的b1数据结构", module.ModuleDescription));
                        this.MessageManager.OnWriteMessageLog(this, args);

                        files = new List <string>();
                        foreach (var item in gpDSItems[gpKey].Where(c => c.ItemType == emDSItemType.data_structure &&
                                                                    c.Platform == emPlatform.b1))
                        {
                            logFile.WriteLine(string.Format("  file:{0}", item.FilePath));
                            files.Add(item.FilePath);
                        }
                        if (files.Count > 0)
                        {
                            var xmlTrans = new XmlTransformer();
                            var b1Model  = xmlTrans.ToDomainModel(files.ToArray());
                            if (xmlTrans.HasError)
                            {
                                throw xmlTrans.ErrorLogs.FirstOrDefault();
                            }

                            var b1Company = this.GetNewCompany();
                            var b1Trans   = new B1DBTransformer();
                            b1Trans.SetDB(b1Company);
                            b1Trans.ToDB(b1Model);
                            b1Company.Disconnect();
                            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(b1Company);
                            if (b1Trans.HasError)
                            {
                                throw new Exception(b1Trans.Errors.ToString());
                            }
                        }
                        #endregion
                        #endregion
                        #region sql脚本
                        args = new Core.ServiceEventArgs(string.Format("正在执行模块[{0}]的sql脚本", module.ModuleDescription));
                        this.MessageManager.OnWriteMessageLog(this, args);

                        files = new List <string>();
                        foreach (var item in gpDSItems[gpKey].Where(c => c.ItemType == emDSItemType.sql_script))
                        {
                            logFile.WriteLine(string.Format("  file:{0}", item.FilePath));
                            files.Add(item.FilePath);
                        }
                        if (files.Count > 0)
                        {
                            foreach (var item in files)
                            {
                                try
                                {
                                    var sqlTrans = new SqlTransformer();
                                    sqlTrans.DBTypeSign = this.AppSetting.DatabaseType;
                                    var sqlMap = new SQLMapFactory(this.AppSetting.DBServer, this.AppSetting.DBUser, this.AppSetting.DBPassword, this.AppSetting.DBName).GetSQLMap(sqlTrans.DBTypeSign);
                                    sqlMap.DefaultDatabase = string.Empty;
                                    sqlTrans.SetDB(new dbConnectionFactory(this.AppSetting.DBServer, this.AppSetting.DBUser, this.AppSetting.DBPassword, this.AppSetting.DBName).GetDBConnection(sqlMap));
                                    sqlTrans.AddScriptFile(item);
                                    sqlTrans.Run();
                                }
                                catch (Exception error)
                                {
                                    if (!item.Contains(string.Format(@"DataStructures\SQLs\SQL_SYS_mssql_Initialization.sql")))
                                    {
                                        throw new Exception(string.Format("执行[{0}]脚本,错误:{1}", item, error.ToString()));
                                    }
                                }
                            }
                        }
                        #endregion
                        logFile.WriteLine();
                    }
                    logFile.Close();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }