comboBox綁定數據庫、模糊查詢

實現:sql

1、綁定數據庫數據庫

點擊查詢按鈕,comboBox顯示從數據庫查到的某字段的一列數據this

方法:在按鈕的點擊事件綁定數據庫spa

private void button1_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
            {
                con.Open();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format("select * from  test t");
                    int rows = cmd.ExecuteNonQuery();
                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    //con.Close();
                    DataTable dt = ds.Tables[0];
                    this.comboBox1.DataSource = dt;
                    this.comboBox1.DisplayMember="name";  //要顯示的數據庫中某字段的一列數據
                    this.comboBox1.ValueMember = "id";  //設置了ValueMember=‘id’

                }
            }


        }

 設置了ValueMember=‘id’,而後前臺頁面就能夠根據comboBox選中的項,獲取其id,code

根據選中的comboBox的項獲取其id值:string str=comboBox1.SelectedValue.ToString();orm

根據選中的comboBox的項獲取其文本值:string str=comboBox1.Text;blog

 

2、模糊查詢(在上面代碼的基礎上加兩句)事件

在comboBox上輸入的字傳給str,sql語句模糊查詢這個str便可cmd

        string str = this.comboBox1.Text;
            using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
            {
                con.Open();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format("select * from  test t where t.name like '%"+str+"%'");
                    int rows = cmd.ExecuteNonQuery();
                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    //con.Close();
                    DataTable dt = ds.Tables[0];
                    this.comboBox1.DataSource = dt;
                    this.comboBox1.DisplayMember = "name";
                    this.comboBox1.ValueMember = "id";
            this.comboBox1.DroppedDown = true;  //點擊查詢,讓comboBox下拉列表展開顯示獲得的結果,
                }
            }

 

3、自動補全後面剩餘字段string

須要綁定數據源到Load方法裏,並且打開頁面就能顯示全部數據,

private void Form1_Load(object sender, EventArgs e)
        {
            using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
            {
                con.Open();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = string.Format("select * from  test t");
                    int rows = cmd.ExecuteNonQuery();
                    SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    //con.Close();
                    DataTable dt = ds.Tables[0];
                    this.comboBox1.DataSource = dt;
                    this.comboBox1.DisplayMember = "name";
                    this.comboBox1.ValueMember = "id";
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  //自動補全後面剩餘字段 this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;  //自動補全後面剩餘字段
} } }
相關文章
相關標籤/搜索