Example #1
0
        private void LoadProfileNames(string defaultValue = null)
        {
            string type = this.cboDbType.Text;

            if (type != "")
            {
                IEnumerable <ConnectionProfileInfo> profiles = ConnectionProfileManager.GetProfiles(type);

                List <string> names = profiles.Select(item => item.Name).ToList();

                this.cboDbProfile.DataSource    = profiles.ToList();
                this.cboDbProfile.DisplayMember = nameof(ConnectionProfileInfo.Description);
                this.cboDbProfile.ValueMember   = nameof(ConnectionProfileInfo.Name);

                if (string.IsNullOrEmpty(defaultValue))
                {
                    if (profiles.Count() > 0)
                    {
                        this.cboDbProfile.SelectedIndex = 0;
                    }
                }
                else
                {
                    if (names.Contains(defaultValue))
                    {
                        this.cboDbProfile.Text = profiles.FirstOrDefault(item => item.Name == defaultValue)?.Description;
                    }
                }

                bool selected = this.cboDbProfile.Text.Length > 0;

                this.btnConfigDbProfile.Visible = this.btnDeleteDbProfile.Visible = selected;
            }
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (!this.ucDbAccountInfo.ValidateInfo())
            {
                return;
            }

            if (string.IsNullOrEmpty(this.cboDatabase.Text))
            {
                MessageBox.Show("Please select a database.");
                return;
            }

            string profileName = this.txtProfileName.Text.Trim();
            string database    = this.cboDatabase.Text;

            this.ConnectionInfo = this.GetConnectionInfo();

            if (!this.NotUseProfile)
            {
                IEnumerable <ConnectionProfileInfo> profiles = ConnectionProfileManager.GetProfiles(this.DatabaseType.ToString());

                Guid?oldAccountProfileId = null;

                if (!string.IsNullOrEmpty(profileName) && profiles.Any(item => item.Name == profileName))
                {
                    string msg = $"The profile name \"{profileName}\" has been existed";

                    if (this.isAdd)
                    {
                        DialogResult dialogResult = MessageBox.Show(msg + ", are you sure to override it.", "Confirm", MessageBoxButtons.YesNo);

                        if (dialogResult != DialogResult.Yes)
                        {
                            this.DialogResult = DialogResult.None;
                            return;
                        }
                    }
                    else if (!this.isAdd && this.ProflieName != profileName)
                    {
                        MessageBox.Show(msg + ", please edit that.");
                        return;
                    }
                    else //edit
                    {
                        oldAccountProfileId = profiles.FirstOrDefault(item => item.Name == profileName).AccountProfileId;
                    }
                }

                ConnectionProfileInfo profile = new ConnectionProfileInfo()
                {
                    ConnectionInfo = this.ConnectionInfo
                };

                if (oldAccountProfileId.HasValue)
                {
                    profile.AccountProfileId = oldAccountProfileId.Value;
                }

                profile.Name         = profileName;
                profile.DatabaseType = this.DatabaseType.ToString();

                this.ProflieName = ConnectionProfileManager.Save(profile, this.ucDbAccountInfo.RememberPassword);
            }

            this.DialogResult = DialogResult.OK;
        }