【问题标题】:Clearing items in Combobox if user selection changed如果用户选择更改,则清除组合框中的项目
【发布时间】:2011-09-29 15:45:50
【问题描述】:

我正在开发一个 windowsform 应用程序。它由一个带有服务器名称列表的组合框和一个用于选定服务器名称的数据库列表和一个用于选定数据库的表列表的组合框组成。当用户选择服务器名称并单击按钮时,它会在服务器中显示数据库名称。如果用户改变主意并选择另一个服务器名称,我仍然拥有相同的第一个选择的数据库列表。我的数据库列表每次都应该刷新,具体取决于服务器选择更改如何实现这一点

这是我的示例代码:

   public MainForm()
    {
        InitializeComponent();
        // FileHelper = new SqlDatabaseDataExport.FileHelper.FileUtilHelper();
        dt = SmoApplication.EnumAvailableSqlServers(false);

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                ServernamesList_combobox.Items.Add(dr["Name"]);

            }
           // ServernamesList_combobox.Items.Add("10.80.104.30\\webx");

            DisplayMainWindow("Server list added");
            Logger.Log("Server List added");
        }

        Authentication_combobox.Items.Add("Windows Authentication");
        Authentication_combobox.Items.Add("Sql Authentication");
    }



    /// <summary>
    /// Generating list of databases with in the selected Server and list of 
    /// selected tables with in the selected 
    /// databse
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    private void DatabasenamesList_combobox_SelectedIndexChanged(object sender, EventArgs e)
    {

        dbName = DatabasenamesList_combobox.SelectedItem.ToString();


        connectionString = GetConnectionString();
        string mySelectQuery = "select [name] from sys.tables WHERE type = 'U' AND is_ms_shipped = 0 ORDER BY [name];";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, con);

        con.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();
        try
        {
            while (myReader.Read())
            {
                SelectTables.Items.Add(myReader.GetString(0));
            }
        }
        finally
        {

            myReader.Close();
            con.Close();
        }


    }



      private void button1_Click_1(object sender, EventArgs e)
    {
        serveName = ServernamesList_combobox.SelectedItem.ToString();
        if (string.IsNullOrEmpty(serveName))
        {
            MessageBox.Show("Please select servername");
            return;
        }

        if (Authentication_combobox.SelectedItem == null)
        {
            MessageBox.Show("Please select authentication");
            return;
        }
        String conxString = string.Empty;

        if (Authentication_combobox.SelectedItem == "Windows Authentication")
        {
            conxString = "Data Source=" + serveName + "; Integrated Security=True;";
        }

        if (Authentication_combobox.SelectedItem == "Sql Authentication")
        {
            if (string.IsNullOrEmpty(Username.Text))
            {
                MessageBox.Show("Please Enter Valid User name");
                return;
            }
            if (string.IsNullOrEmpty(Password.Text))
            {
                MessageBox.Show("Please Enter Valid Password");
                return;
            }
            conxString = "Data Source=" + serveName + "; Integrated Security=False;User ID =" + Username.Text + ";Password=" + Password.Text;
        } 


        using (SqlConnection sqlConx = new SqlConnection(conxString))
        {

            try
            {
                sqlConx.Open();
                MessageBox.Show("Connection established successfully");

            }
            catch (Exception ex)
            {

                MessageBox.Show("Exception" + ex);
                MessageBox.Show(" Please enter valid Credentials");
                return;

            }

            DataTable tblDatabases = sqlConx.GetSchema("Databases");
            sqlConx.Close();

            foreach (DataRow row in tblDatabases.Rows)
            {
                Databases.Add(row["database_name"].ToString());
            }

        foreach (var database in Databases)
        {
            DatabasenamesList_combobox.Items.Add(database);
        }


    }

  }

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    将 SelectedIndexChanged 事件添加到您的 DatabasenamesList_combobox。 在该方法的代码中,只需调用您的代码来填充您的数据库。现在一切都被塞进'button1'。将您的程序移到一个称为

    的程序中 private void PopulateDatabases(string serverName) { //populate Databases //.. your code here ... //clear the list DatabasenamesList.Items.Clear(); foreach (var database in Databases) { DatabasenamesList_combobox.Items.Add(database); } }

    还有许多其他方法可以清理此代码,例如在您的: 捕捉(例外)

    你怎么知道异常是因为没有正确的身份验证类型?您应该捕获特定类型的异常并单独处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2016-03-03
      • 2012-09-23
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多