Esempio n. 1
0
 public SqlServerInfoModel(SqlServerInfo info)
     : this()
 {
     Name      = info.Name;
     StartMode = info.StartMode;
     State     = info.State;
     Status    = info.Status;
 }
Esempio n. 2
0
        public IServerDriver GetDriver(SqlServerInfo server)
        {
            var driver = _drivers.SingleOrDefault(x => x.CanHandle(server.ServerDriver));

            if (driver == null)
            {
                throw new Exception("Unknown ServerDriver: " + server.ServerDriver);
            }
            return(driver);
        }
Esempio n. 3
0
        public string BuildConnectionString(SqlServerInfo serverInfo)
        {
            var result = "Data Source=" + serverInfo.DataSource + ";";

            if (!string.IsNullOrEmpty(serverInfo.SqlPassword))
            {
                result += "Password="******";";
            }

            return(result);
        }
Esempio n. 4
0
        public TreeNode GetServerPathAsTreeNode()
        {
            TreeNode startTreeNode = new TreeNode();

            startTreeNode.Text = SqlServerInfo.GetSqlServerVersion(_connectionString);

            foreach (TreeNode node in GetServerPath())
            {
                startTreeNode.Nodes.Add(node);
            }

            return(startTreeNode);
        }
Esempio n. 5
0
        public static SqlServerInfo LoginAck(this TdsPackageReader reader)
        {
            var sqlServerInfo = new SqlServerInfo();

            // read past interface type and version
            reader.ReadByte();

            var b          = reader.GetBytes(TdsEnums.VERSION_SIZE);
            var tdsVersion = (uint)((((((b[0] << 8) | b[1]) << 8) | b[2]) << 8) | b[3]); // bytes are in motorola order (high byte first)
            var majorMinor = tdsVersion & 0xff00ffff;
            var increment  = (tdsVersion >> 16) & 0xff;

            // Server responds:
            // 0x07000000 -> Sphinx         // Notice server response format is different for bwd compat
            // 0x07010000 -> Shiloh RTM     // Notice server response format is different for bwd compat
            // 0x71000001 -> Shiloh SP1
            // 0x72090002 -> Yukon RTM
            // 0x730B0003 -> Katmai RTM
            // 0x74000004 -> DENALI RTM
            // information provided by S. Ashwin

            switch (majorMinor)
            {
            case (TdsEnums.YUKON_MAJOR << 24) | TdsEnums.YUKON_RTM_MINOR when increment == TdsEnums.YUKON_INCREMENT:
                sqlServerInfo.IsYukon = true;
                break;

            case (TdsEnums.KATMAI_MAJOR << 24) | TdsEnums.KATMAI_MINOR when increment == TdsEnums.KATMAI_INCREMENT:
                sqlServerInfo.IsYukon         = true;
                sqlServerInfo.IsKatmaiOrNewer = true;
                break;

            case (TdsEnums.DENALI_MAJOR << 24) | TdsEnums.DENALI_MINOR when increment == TdsEnums.DENALI_INCREMENT:
                sqlServerInfo.IsYukon         = true;
                sqlServerInfo.IsKatmaiOrNewer = true;
                sqlServerInfo.IsDenali        = true;
                break;

            default:
                throw SQL.InvalidTdsVersion();
            }
            //isYukon is always true otherwise we send an exception

            var len = reader.ReadByte();

            sqlServerInfo.Name         = reader.ReadUnicodeChars(len * 2);
            sqlServerInfo.MajorVersion = reader.ReadByte();
            sqlServerInfo.MinorVersion = reader.ReadByte();
            sqlServerInfo.BuildNum     = (short)((reader.ReadByte() << 8) + reader.ReadByte());
            return(sqlServerInfo);
        }
Esempio n. 6
0
        public void ShouldReturnReasonableDataDirectory()
        {
            var context = new TestDbContext();
            var sqlServerInfo = new SqlServerInfo(context);

            var actualBackupDirectory = sqlServerInfo.GetBackupDirectory();
            var actualDataDirectory = sqlServerInfo.GetDataDirectory();
            var acutalLogDirectory = sqlServerInfo.GetLogDirectory();

            var escapedPrefix = Regex.Escape(@"C:\Program Files\Microsoft SQL Server\MSSQL");

            RegexAssert.IsMatch(@"^" + escapedPrefix + @".*\.MSSQLSERVER\\MSSQL\\Backup", actualBackupDirectory);
            RegexAssert.IsMatch(@"^" + escapedPrefix + @".*\.MSSQLSERVER\\MSSQL\\DATA", actualDataDirectory);
            RegexAssert.IsMatch(@"^" + escapedPrefix + @".*\.MSSQLSERVER\\MSSQL\\DATA", acutalLogDirectory);
        }
 private void executeDbMethodWithSpecifiedDatabaseInfo(SqlServerInfo info, Action <DBConnection> method)
 {
     executeMethodWithDbExceptionHandling(
         () => {
         var connection = new DBConnection(
             new SqlServerInfo(
                 (info as DatabaseInfo).SecondaryDatabaseName,
                 info.Server,
                 info.LoginName,
                 info.Password,
                 info.Database,
                 false,
                 info.FullTextCatalog));
         connection.ExecuteWithConnectionOpen(() => method(connection));
     });
 }
        public string BuildConnectionString(SqlServerInfo serverInfo)
        {
            var result = "Data Source=" + serverInfo.DataSource + ";";

            if (serverInfo.UseIntegratedSecurity)
            {
                result += "Integrated Security=SSPI;";
            }
            else
            {
                result += $"User Id={serverInfo.SqlUsername};Password={serverInfo.SqlPassword};";
            }

            if (_config.ConnectionTimeoutInSeconds != null)
            {
                result += "Connect Timeout=" + _config.ConnectionTimeoutInSeconds + ";";
            }

            return(result);
        }
Esempio n. 9
0
 public LoginForm(IServerAuthentication serverAuthentication)
 {
     InitializeComponent();
     string[] dataSourceNames = null;
     try
     {
         dataSourceNames = SqlServerInfo.GetDataSourceName().ToArray();
         serverName_Dropdown.Items.AddRange(dataSourceNames);
         authenticationType_ComboBox.SelectedIndex = 0;
         serverName_Dropdown.SelectedIndex         = 0;
         _serverAuthentication = serverAuthentication;
     }
     catch (Exception)
     {
         if (dataSourceNames is null)
         {
             MessageBoxWrapper.ShowErrorBox("None Sql Server Instances", "No Sql Server Instances were found in your computer");
         }
     }
 }
Esempio n. 10
0
        // 获得 SQL Server 信息
        // return:
        //      -1  出错
        //      0   放弃
        //      1   成功
        public int GetSqlServerInfo(
            string strSqlServerName,
            out SqlServerInfo info,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            info = new SqlServerInfo();

            REDO_INPUT:
            SaLoginDialog dlg = new SaLoginDialog();
            GuiUtil.AutoSetDefaultFont(dlg);
            dlg.SqlServerName = strSqlServerName;
            dlg.StartPosition = FormStartPosition.CenterScreen;

            dlg.ShowDialog(this);

            if (dlg.DialogResult != DialogResult.OK)
                return 0;

            info.ServerName = strSqlServerName;
            info.SqlUserName = dlg.SqlUserName;
            info.SqlUserPassword = dlg.SqlPassword;
            info.SSPI = dlg.SSPI;

            string strConnection = @"Persist Security Info=False;"
                + "User ID=" + info.SqlUserName + ";"    //帐户和密码
                + "Password="******";"
                + "Data Source=" + strSqlServerName + ";"
                + "Connect Timeout=30";

            if (info.SSPI == true)
            {
                strConnection = @"Persist Security Info=False;"
                    + "Integrated Security=SSPI; "      //信任连接
                    + "Data Source=" + strSqlServerName + ";"
                    + "Connect Timeout=30"; // 30秒
            }

            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(strConnection);
            }
            catch (Exception ex)
            {
                strError = "建立连接时出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                return -1;
            }

            try
            {
                connection.Open();
            }
            catch (SqlException sqlEx)
            {
                strError = "连接SQL服务器出错:" + sqlEx.Message + "。";
                int nError = sqlEx.ErrorCode;
                MessageBox.Show(this, strError);
                goto REDO_INPUT;
                return -1;
            }
            catch (Exception ex)
            {
                strError = "连接SQL服务器出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                return -1;
            }

            /* http://support.microsoft.com/kb/321185
             * 10 -- SQL Server 2008
             * 9 -- SQL Server 2005
             * 8 -- SQL 2000
             * 7 -- SQL 7.0
             * */

            try
            {
                string strVersion = "7";
                string strCommand = "";
                SqlCommand command = null;

                // Debug.Assert(false, "");

                strCommand = "SELECT SERVERPROPERTY('productversion')";
                command = new SqlCommand(strCommand,
                    connection);
                try
                {
                    strVersion = (string)command.ExecuteScalar();
                    // 去掉次要版本号
                    nRet = strVersion.IndexOf(".");
                    if (nRet != -1)
                        strVersion = strVersion.Substring(0, nRet);
                }
                catch (Exception /*ex*/)
                {
                    // strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                    // return -1;
                    strVersion = "7";
                }

                info.Version = strVersion;

                strCommand = "SELECT SERVERPROPERTY('IsIntegratedSecurityOnly')";
                command = new SqlCommand(strCommand,
                    connection);
                try
                {
                    nRet = (Int32)command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    //strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                    //return -1;
                    nRet = 1;
                }

                if (nRet == 1)
                    info.IntegratedSecurityOnlyMode = true;
                else
                    info.IntegratedSecurityOnlyMode = false;

            }
            finally
            {
                connection.Close();
            }

            return 0;
        }
 public SqlServer(SqlServerInfo info, string dataLogicalFileName, string logLogicalFileName)
 {
     this.info = info;
     this.dataLogicalFileName = dataLogicalFileName;
     this.logLogicalFileName  = logLogicalFileName;
 }
Esempio n. 12
0
 public SqlServer(SqlServerInfo info) => this.info = info;
Esempio n. 13
0
        // 获得 SQL Server 信息
        // return:
        //      -1  出错
        //      0   放弃
        //      1   成功
        public int GetSqlServerInfo(
            string strSqlServerName,
            out SqlServerInfo info,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            info = new SqlServerInfo();

REDO_INPUT:
            SaLoginDialog dlg = new SaLoginDialog();

            GuiUtil.AutoSetDefaultFont(dlg);
            dlg.SqlServerName = strSqlServerName;
            dlg.StartPosition = FormStartPosition.CenterScreen;

            dlg.ShowDialog(this);

            if (dlg.DialogResult != DialogResult.OK)
            {
                return(0);
            }

            info.ServerName      = strSqlServerName;
            info.SqlUserName     = dlg.SqlUserName;
            info.SqlUserPassword = dlg.SqlPassword;
            info.SSPI            = dlg.SSPI;

            string strConnection = @"Persist Security Info=False;"
                                   + "User ID=" + info.SqlUserName + ";" //帐户和密码
                                   + "Password="******";"
                                   + "Data Source=" + strSqlServerName + ";"
                                   + "Connect Timeout=30";

            if (info.SSPI == true)
            {
                strConnection = @"Persist Security Info=False;"
                                + "Integrated Security=SSPI; " //信任连接
                                + "Data Source=" + strSqlServerName + ";"
                                + "Connect Timeout=30";        // 30秒
            }

            SqlConnection connection = null;

            try
            {
                connection = new SqlConnection(strConnection);
            }
            catch (Exception ex)
            {
                strError = "建立连接时出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                return(-1);
            }

            try
            {
                connection.Open();
            }
            catch (SqlException sqlEx)
            {
                strError = "连接SQL服务器出错:" + sqlEx.Message + "。";
                int nError = sqlEx.ErrorCode;
                MessageBox.Show(this, strError);
                goto REDO_INPUT;
                return(-1);
            }
            catch (Exception ex)
            {
                strError = "连接SQL服务器出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                return(-1);
            }

            /* http://support.microsoft.com/kb/321185
             * 10 -- SQL Server 2008
             * 9 -- SQL Server 2005
             * 8 -- SQL 2000
             * 7 -- SQL 7.0
             * */

            try
            {
                string     strVersion = "7";
                string     strCommand = "";
                SqlCommand command    = null;

                // Debug.Assert(false, "");

                strCommand = "SELECT SERVERPROPERTY('productversion')";
                command    = new SqlCommand(strCommand,
                                            connection);
                try
                {
                    strVersion = (string)command.ExecuteScalar();
                    // 去掉次要版本号
                    nRet = strVersion.IndexOf(".");
                    if (nRet != -1)
                    {
                        strVersion = strVersion.Substring(0, nRet);
                    }
                }
                catch (Exception /*ex*/)
                {
                    // strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                    // return -1;
                    strVersion = "7";
                }

                info.Version = strVersion;

                strCommand = "SELECT SERVERPROPERTY('IsIntegratedSecurityOnly')";
                command    = new SqlCommand(strCommand,
                                            connection);
                try
                {
                    nRet = (Int32)command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    //strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString();
                    //return -1;
                    nRet = 1;
                }

                if (nRet == 1)
                {
                    info.IntegratedSecurityOnlyMode = true;
                }
                else
                {
                    info.IntegratedSecurityOnlyMode = false;
                }
            }
            finally
            {
                connection.Close();
            }

            return(0);
        }
Esempio n. 14
0
        private void button_OK_Click(object sender, EventArgs e)
        {
            string strError = "";
            int    nRet     = 0;

            if (this.textBox_sqlServerName.Text == "")
            {
                strError = "尚未指定 SQL 服务器";
                goto ERROR1;
            }

            if (string.Compare(this.textBox_sqlServerName.Text.Trim(), "~sqlite") == 0)
            {
                strError = "MS SQL 服务器名不能为 '~sqlite',因为这个名字保留给了 SQLite 内置数据库类型";
                goto ERROR1;
            }

#if NO
            // 获得 SQL Server 信息
            SqlServerInfo info = null;
            nRet = GetSqlServerInfo(
                this.SqlServerName,
                dlg.SqlUserName,
                dlg.SqlPassword,
                dlg.SSPI,
                out info,
                out strError);
            if (nRet == -1)
            {
                goto ERROR1;
            }
#endif

            nRet = GetIntegratedSecurityOnlyMode(this.textBox_sqlServerName.Text, out strError);
            if (nRet == -1)
            {
                MessageBox.Show(this, strError);
            }

            bool bISOM = false;
            if (nRet == 1)
            {
                bISOM = true;
            }

            // 集成权限登录唯一方式的情况下,不要创建 登录名
            if (bISOM == true)
            {
                this.textBox_loginName.Text            = "";
                this.textBox_loginPassword.Text        = "";
                this.textBox_confirmLoginPassword.Text = "";
            }
            else
            {
                if (this.textBox_loginName.Text == "")
                {
                    strError = "尚未指定 dp2Kernel 登录名";
                    goto ERROR1;
                }

                if (this.textBox_loginPassword.Text != this.textBox_confirmLoginPassword.Text)
                {
                    strError = "dp2Kernel 登录名的密码和确认密码不一致";
                    goto ERROR1;
                }
            }

            /*
             * if (this.SSPI == false && this.textBox_sqlUserName.Text == "")
             * {
             *  MessageBox.Show(this, "尚未指定SQL帐号。");
             *  return;
             * }
             *
             *
             * // 检测SQL帐户是否正确
             * EnableControls(false);
             * string strError = "";
             * int nRet = this.detect(this.textBox_sqlServerName.Text,
             *  this.textBox_sqlUserName.Text,
             *  this.textBox_sqlPassword.Text,
             *  radioButton_SSPI.Checked,
             *  out strError);
             * EnableControls(true);
             * if (nRet == -1)
             * {
             *  strError = strError + "\r\n" + "请重新指定服务器信息。";
             *  MessageBox.Show(this, strError);
             *  return;
             * }
             * */

            SaLoginDialog dlg = new SaLoginDialog();
            GuiUtil.AutoSetDefaultFont(dlg);
            dlg.SqlServerName = this.textBox_sqlServerName.Text;
            dlg.StartPosition = FormStartPosition.CenterScreen;

            dlg.ShowDialog(this);

            if (dlg.DialogResult != DialogResult.OK)
            {
                return;
            }

            if (string.IsNullOrEmpty(this.textBox_loginName.Text) == false)
            {
                // 创建dp2Kernel登录名

                // 创建一个适合于dpKernel的SQL Server login
                // return:
                //      -1  出错
                //      0   成功
                //      1   原来已经存在,且不允许删除
                nRet = CreateLogin(
                    this.SqlServerName,
                    dlg.SqlUserName,
                    dlg.SqlPassword,
                    dlg.SSPI,
                    this.textBox_loginName.Text,
                    this.textBox_loginPassword.Text,
                    out strError);
                if (nRet == -1 || nRet == 1)
                {
                    goto ERROR1;
                }
            }

            if (bISOM == true)
            {
                nRet = AddSystemDbCreatorRole(
                    this.SqlServerName,
                    dlg.SqlUserName,
                    dlg.SqlPassword,
                    dlg.SSPI,
                    out strError);
                if (nRet == -1)
                {
                    strError = "为 登录名 'NT AUTHORITY\\SYSTEM' 添加 'dbcreator' 时出错: " + strError;
                    goto ERROR1;
                }
            }

            /*
             * if (nRet == 1)
             * {
             *  string strText = "登录名 '" + this.textBox_loginName.Text + "' 在SQL服务器 '" + this.SqlServerName + "' 中已经存在,但其密码不一定和当前指定的密码相同。\r\n\r\n是否继续使用这个登录名?\r\n(Yes)继续使用;(No)重新指定登录名和密码";
             *  DialogResult result = MessageBox.Show(this,
             *      strText,
             *      "setup_dp2Kernel",
             *      MessageBoxButtons.YesNo,
             *      MessageBoxIcon.Question,
             *      MessageBoxDefaultButton.Button2);
             *  if (result == DialogResult.No)
             *  {
             *      this.textBox_loginPassword.Focus();
             *      return;
             *  }
             * }
             * */

            this.DialogResult = DialogResult.OK;
            this.Close();
            return;

ERROR1:
            MessageBox.Show(this, strError);
            MessageBox.Show(this, "虽然刚才的创建登录名操作失败了,但您也可以在重新指定登录名和密码后,再次按“确定”按钮创建登录名,继续进行安装");
        }
Esempio n. 15
0
 private void executeDbMethodWithSpecifiedDatabaseInfo(SqlServerInfo info, Action <DBConnection> method) =>
 executeMethodWithDbExceptionHandling(
     () => {
     var connection = new DBConnection(new SqlServerInfo(info.ConnectionString));
     connection.ExecuteWithConnectionOpen(() => method(connection));
 });