使用两个列表框下拉菜单从数据库中选择标题。当两个都选中时,我可以读取其他两个,但是当一个列表框为null时,即使那是null,它也始终自动运行最后一个(RatingListBox.SelectedIndex == -1)节。
我希望能够同时选择“ EITHER / OR”和“两者”,并返回一个消息框,用于在单击“搜索”按钮时未选择任何框。
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
MovieList.Items.Clear();
string connectionstring = null;
string sql = null;
SqlConnection connection;
SqlCommand command;
SqlDataReader datareader;
//string Genre = GenreListBox?.SelectedValue.ToString();
//string Rating = RatingListBox?.SelectedValue.ToString();
string Director = DirectorTextbox.ToString();
string Actor = ActorTextbox.ToString();
InitializeComponent();
connectionstring = "Data Source=******;" +
"Initial Catalog=***********;" +
"User ID=****************" +
"Password=*******";
if (GenreListBox.SelectedIndex == -1 && RatingListBox.SelectedIndex == -1)
{
string Genre = GenreListBox?.SelectedValue.ToString();
string Rating = RatingListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Genre = '" + Genre + "' AND Rating = '" + Rating + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else if (GenreListBox.SelectedIndex == -1)
{
string Genre = GenreListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Genre = '" + Genre + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else if (RatingListBox.SelectedIndex == -1)
{
string Rating = RatingListBox?.SelectedValue.ToString();
connection = new SqlConnection(connectionstring);
sql = "SELECT DVD_Title FROM Bluebox_Titles WHERE Rating = '" + Rating + "'";
connection.Open();
command = new SqlCommand(sql, connection);
datareader = command.ExecuteReader();
while (datareader.Read())
{
MovieList.Items.Add(datareader[0]);
}
datareader.Close();
command.Dispose();
connection.Close();
}
else
{ MessageBox.Show("Please Select Genre And/Or Rating before Searching Titles!"); }