我只需要在下拉列表中显示包含“ R2P”的数据库。
我尝试在数据库列表中选择“包含”,但没有成功。
private void DbComboBox_DropDown(object sender, EventArgs e)
{
string connectionString;
if (this.windowsAuthentication.Checked)
connectionString = string.Format("Server={0}; Integrated Security=SSPI;", sqlServerComboBox.Text);
else
connectionString = string.Format("Server={0}; User ID={1}; Password={2};", sqlServerComboBox.Text, loginTexBox.Text, passwordTextBox.Text);
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
DataTable databaseList = sqlConnection.GetSchema("Databases");
sqlConnection.Close();
this.dbComboBox.Items.Clear();
foreach (DataRow row in databaseList.Rows)
this.dbComboBox.Items.Add(row["database_name"]);
}
}
答案 0 :(得分:0)
您可以这样做:
foreach (DataRow row in databaseList.Rows)
{
if (row["database_name"].ToString().Contains("R2P"))
{
this.dbComboBox.Items.Add(row["database_name"]);
}
}
我也建议抛开字符串。格式化并改为使用字符串插值:
connectionString = $"Server={sqlServerComboBox.Text}; User ID={loginTexBox.Text}; Password={passwordTextBox.Text};";