c#项目夜总会

本文关键字:夜总会 项目 | 更新日期: 2023-09-27 18:08:02

大家好,我正在用c#构建一个包含夜总会记录的项目。用户将能够通过他们的标准搜索夜总会,这些标准可以是:夜总会播放的音乐或夜总会提供的饮料等。我正在构建一个表单应用程序,该表单包含4个组合框,用户可以设置他们的标准来搜索夜总会和显示数据库记录的datagridview,但我有以下问题看看我的代码:

public partial class KirioMenou : Form
{
    SqlCommand cmd;
    SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)'MSSQLLocalDB; AttachDbFilename = c:'users'dim'documents'visual studio 2015'Projects'Eksatomikeush'Eksatomikeush'Xrhstes.mdf; Integrated Security = True");
    SqlDataAdapter ad;
    public KirioMenou()
    {          
        InitializeComponent();
        fillcombo();
    }
    // this is a logout button
    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 ss = new Form1();
        ss.Show();
    }
    private void KirioMenou_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'xrhstesDataSet.Magazia' table. You can move, or remove it, as needed.
        this.magaziaTableAdapter.Fill(this.xrhstesDataSet.Magazia);
    }
     void fillcombo()
    {
        //Type
        comboBox1.Items.Add("Club");
        comboBox1.Items.Add("Bar");
        //type of music        
        comboBox2.Items.Add("Rock");
        comboBox2.Items.Add("House");
        comboBox2.Items.Add("Elliniki");
        comboBox2.Items.Add("Industrial");
        //place
        comboBox3.Items.Add("Aigaleo");
        comboBox3.Items.Add("Peristeri");
        comboBox3.Items.Add("Gazi");
        comboBox3.Items.Add("Peiraias");
        //kind of drink
        comboBox4.Items.Add("beer");
        comboBox4.Items.Add("All");
        comboBox4.Items.Add("Whiskey");

    }
    //this is the button that i call the "showdata()"
    //Also the form contains a textbox if someone wants
    //to search a nightclub by name
    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            showdata();   
        }
        else
        {
            con.Open();
            DataTable dt = new DataTable();
            ad = new SqlDataAdapter("SELECT * FROM Magazia WHERE Onoma='" + textBox1.Text + "'", con);
            ad.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
    }
     //and this is just a reset button of comboxes and textbox
     private void button3_Click(object sender, EventArgs e)
    {
        con.Open();
        DataTable dt = new DataTable();
        ad = new SqlDataAdapter("SELECT * FROM Magazia", con);
        ad.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
        textBox1.Text = "";
        comboBox1.Text = "";
        comboBox2.Text = "";
        comboBox3.Text = "";
        comboBox4.Text = "";
    }
    void showdata()
    {
        //declare parameters
        SqlParameter paraType = new SqlParameter("@Type", SqlDbType.VarChar);
        SqlParameter paraMusic = new SqlParameter("@Music", SqlDbType.VarChar);
        SqlParameter paraPlace = new SqlParameter("@Place", SqlDbType.VarChar);
        SqlParameter paraDrink = new SqlParameter("@Drink", SqlDbType.VarChar);
        //append the parameters
        if (string.IsNullOrEmpty(comboBox1.Text))
        {
            paraType.Value = DBNull.Value;
        }
        else
        {
            paraType.Value = comboBox1.Text;
        }
        if (string.IsNullOrEmpty(comboBox2.Text))
        {
            paraMusic.Value = DBNull.Value;
        }
        else
        {
            paraMusic.Value = comboBox2.Text;
        }
        if (string.IsNullOrEmpty(comboBox3.Text))
        {
            paraPlace.Value = DBNull.Value;
        }
        else
        {
            paraPlace.Value = comboBox3.Text;
        }
        if (string.IsNullOrEmpty(comboBox4.Text))
        {
            paraDrink.Value = DBNull.Value;
        }
        else
        {
            paraDrink.Value = comboBox4.Text;
        }
        //construct the query
        con.Open();
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM Magazia WHERE Type = @Type AND Music = @Music AND Place = @Place AND Drink = @Drink";
        cmd.Parameters.Add(paraType);
        cmd.Parameters.Add(paraMusic);
        cmd.Parameters.Add(paraPlace);
        cmd.Parameters.Add(paraDrink);
        //get the data and put it into the datagrid
        dataGridView1.DataSource = cmd.ExecuteReader();
        //tidy up
        cmd.Dispose();
        con.Close();
        con.Dispose();
    }

当我执行我的程序时,它不像我想的那样工作…例如,如果我在运行时在combobox1中设置单词"Club",那么datagridview将正确地选择包含数据库中单词"Club"的单元格。但是当我在其他组合框中设置一个值时,如combobox4,数据网格视图将显示包含特定类型饮料的夜总会,就像它忘记了我在combobox1中选择了"俱乐部",我知道这就是它应该如何与那种代码一起工作,但我希望数据网格视图将显示包含"俱乐部"answers"啤酒"的记录。我认为使用命令行,如:

"SELECT Count(*) FROM Nightclubs WHERE Type='" + combobox1.Text+"'and Drink='"+combobox4.Text+"'",con );

但我仍然要包括很多的"如果"为combobox2和combobox3也使这个工作,我不确定它是否会正确工作。是否有一种简单的方法可以包含用户通过组合框搜索夜总会的所有可能性?

感谢您的帮助

c#项目夜总会

每次代码运行时,都将数据网格的数据源设置为一组新数据,这意味着它将删除旧数据。您需要做的不是将每个组合视为单独的查询,而是将它们连接为1,然后设置数据源。

像这样的东西应该可以做到,但它确实依赖于你的列不包含null !
void showdata() {
    //declare parameters
    SqlParameter paraType = new SqlParameter("@Type", SqlDbType.VarChar);
    SqlParameter paraMusic = new SqlParameter("@Music", SqlDbType.VarChar);
    SqlParameter paraPlace = new SqlParameter("@Place", SqlDbType.VarChar);
    SqlParameter paraDrink = new SqlParameter("@Drink", SqlDbType.VarChar);
    //append the parameters
    if (string.IsNullOrEmpty(comboBox1.Text)) {
        paraType.Value = DBNull.Value;
    } else {
        paraType.Value = comboBox1.Text;
    }
    if (string.IsNullOrEmpty(comboBox2.Text)) {
        paraMusic.Value = DBNull.Value;
    } else {
        paraMusic.Value = comboBox2.Text;
    }
    if (string.IsNullOrEmpty(comboBox3.Text)) {
        paraPlace.Value = DBNull.Value;
    } else {
        paraPlace.Value = comboBox3.Text;
    }
    if (string.IsNullOrEmpty(comboBox4.Text)) {
        paraDrink.Value = DBNull.Value;
    } else {
        paraDrink.Value = comboBox4.Text;
    }
    //construct the query
    con.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText = "SELECT * FROM Magazia WHERE Type = @Type AND Music = @Music AND Place = @Place AND Drink = @Drink";
    com.Parameters.Add(paraType);
    com.Parameters.Add(paraMusic);
    com.Parameters.Add(paraPlace);
    com.Parameters.Add(paraDrink);
    //get the data and put it into the datagrid
    dataGridView1.DataSource = com.ExecuteReader();
    //tidy up
    com.Dispose();
    con.Close();
    con.Dispose();
}