Example #1
0
        private ResultEx CheckSqlServerParameter(InstallSqlServerModel model)
        {
            if (model.ConnectionString != null)
            {
                model.ConnectionString = model.ConnectionString.Trim();
            }
            if (model.ConnectionType == ConnectionType.String)
            {
                //自定义连接字符串
                if (string.IsNullOrEmpty(model.ConnectionString))
                {
                    return(new ResultEx("数据库字符串不能为空"));
                }
                try
                {
                    //尝试构建连接对象
                    var builder = new SqlConnectionStringBuilder(model.ConnectionString);
                    return(new ResultEx(true, "", builder.InitialCatalog));
                }
                catch (Exception ex)
                {
                    return(new ResultEx($"数据库字符串格式不正确:{ex.Message}"));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(model.ServerName))
                {
                    return(new ResultEx("数据库实例地址不能为空"));
                }
                if (string.IsNullOrEmpty(model.DatabaseName))
                {
                    return(new ResultEx("数据库名称不能为空"));
                }

                //授权账号类型
                if (model.AuthenticationType == AuthenticationType.SqlServer)
                {
                    if (string.IsNullOrEmpty(model.Username))
                    {
                        return(new ResultEx("SQL Server用户名不能为空"));
                    }
                    if (string.IsNullOrEmpty(model.Password))
                    {
                        return(new ResultEx("SQL Server密码不能为空"));
                    }
                }
                return(new ResultEx(true, "", model.DatabaseName));
            }
        }
Example #2
0
        public ResultEx ConnectTest(InstallSqlServerModel sqlServerModel)
        {
            var result = CheckSqlServerParameter(sqlServerModel);

            if (result.Flag)
            {
                string connectionString;
                if (sqlServerModel.ConnectionString != null)
                {
                    sqlServerModel.ConnectionString = sqlServerModel.ConnectionString.Trim();
                }
                if (sqlServerModel.ConnectionType == ConnectionType.String)
                {
                    connectionString = sqlServerModel.ConnectionString;
                }
                else
                {
                    connectionString = CreateConnectionString(
                        sqlServerModel.AuthenticationType == AuthenticationType.Windows,
                        sqlServerModel.ServerName, sqlServerModel.DatabaseName, sqlServerModel.Username,
                        sqlServerModel.Password);
                }
                try
                {
                    //通过连接数据库方式确定
                    using (var conn = new SqlConnection(connectionString))
                    {
                        conn.Open();
                        conn.Close();
                    }
                    return(ResultEx.Init(true));
                }
                catch (Exception ex)
                {
                    return(ResultEx.Init(false, ex.Message));
                }
            }

            return(result);
        }
Example #3
0
        public async Task <ResultEx> InitDataBase(InstallSqlServerModel sqlServerModel)
        {
            var result = CheckSqlServerParameter(sqlServerModel);

            if (result.Flag)
            {
                _dataBaseName = result.Data.ToString();
                string connectionString;
                if (!string.IsNullOrEmpty(sqlServerModel.ConnectionString))
                {
                    sqlServerModel.ConnectionString = sqlServerModel.ConnectionString.Trim();
                }
                if (sqlServerModel.ConnectionType == ConnectionType.String)
                {
                    connectionString = sqlServerModel.ConnectionString;
                }
                else
                {
                    connectionString = CreateConnectionString(
                        sqlServerModel.AuthenticationType == AuthenticationType.Windows,
                        sqlServerModel.ServerName, sqlServerModel.DatabaseName, sqlServerModel.Username,
                        sqlServerModel.Password);
                }

                //创建新的DBContext对象只是数据库创建操作
                var builder = new DbContextOptionsBuilder();
                builder.UseSqlServer(connectionString);
                var dbContext       = new Kj1012Context(builder.Options);
                var dataBase        = dbContext.Database;
                var databaseCreator = dataBase.GetService <IRelationalDatabaseCreator>();
                sqlServerModel.ConnectionString = connectionString;
                var isCreateDatabase = false;
                if (sqlServerModel.AlwaysCreate)
                {
                    if (databaseCreator.Exists())
                    {
                        await databaseCreator.DeleteAsync();
                    }
                    await CreateDataBase(databaseCreator, dataBase);

                    isCreateDatabase = true;
                }
                else if (sqlServerModel.NotExistCreate)
                {
                    if (!databaseCreator.Exists())
                    {
                        await CreateDataBase(databaseCreator, dataBase);

                        isCreateDatabase = true;
                    }
                }
                else
                {
                    if (!databaseCreator.Exists())
                    {
                        return(ResultEx.Init(false, "数据库名称不存在,请核对"));
                    }
                }
                var version = GetSqlServerVersion(sqlServerModel.ConnectionString);
                if (version < 10)
                {
                    return(ResultEx.Init(false, "创建数据表失败,Sql Server 数据库版本至少需要Sql Server 2008及以上"));
                }
                if (version > 0)
                {
                    //SQL Server2016 及以上执行2016脚本
                    if (version >= 13 && isCreateDatabase)
                    {
                        //执行2016脚本
                        ExecuteSqlScriptFromFile(dataBase, $"{SqlServerScriptPath}\\SqlServer_2016.sql");
                    }
                    //保存数据库配置信息
                    _appSettingService.SaveDataSetting(new DataSettings
                    {
                        DataProvider     = DataProviderType.SqlServer,
                        Version          = version,
                        ConnectionString = AesHelper.AesEncrypt(sqlServerModel.ConnectionString, ConstDefine.DataSettingAesKey)
                    });
                }
                else
                {
                    return(ResultEx.Init(false, "创建数据表失败,未知SQL SERVER数据库版本"));
                }
            }

            return(result);
        }