1
我正在開發一個windowsform應用程序。它由一個帶有服務器名稱列表的組合框和一個用於所選服務器名稱的數據庫列表組成,另外一個用於所選數據庫的列表。當用戶選擇servername並單擊按鈕時,它將在服務器中顯示數據庫名稱。如果用戶改變了主意並選擇了另一個服務器名稱,我仍然有相同的首先選擇的數據庫列表。我的數據庫列表應該每次刷新取決於服務器種選擇改變我怎樣才能實現這一目標如果用戶選擇發生變化,清除Combobox中的項目
這裏是我的示例代碼:
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);
}
}
}