实现:

一、绑定数据库

点击查询按钮,comboBox显示从数据库查到的某字段的一列数据

方法:在按钮的点击事件绑定数据库

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[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember="name";  //要显示的数据库中某字段的一列数据
this.comboBox1.ValueMember = "id";  //设置了ValueMember=‘id’ }
} }

设置了ValueMember=‘id’,然后前台页面就可以根据comboBox选中的项,获取其id,

根据选中的comboBox的项获取其id值:string str=comboBox1.SelectedValue.ToString();

根据选中的comboBox的项获取其文本值:string str=comboBox1.Text;

二、模糊查询(在上面代码的基础上加两句)

在comboBox上输入的字传给str,sql语句模糊查询这个str即可

        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[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
            this.comboBox1.DroppedDown = true;  //点击查询,让comboBox下拉列表展开显示得到的结果,
                }
}

三、自动补全后面剩余字段

需要绑定数据源到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[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;  //自动补全后面剩余字段
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;  //自动补全后面剩余字段
} } }
												

comboBox绑定数据库、模糊查询的更多相关文章

  1. silverlight中 ComboBox绑定数据库,并获取当前选定值

    silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...

  2. 老生常谈combobox和combotree模糊查询

    FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...

  3. python 操作mongodb数据库模糊查询

    # -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...

  4. ext.net中ComboBox空间实现模糊查询

    ComboBox中的属性添加Mode="Local"可以实现第一个字的模糊查询但是搜索中间的字无法实现 现提供一下方法使用正则表达式实现全模糊查询 <ext:ComboBox ...

  5. 2019-2-14sql server数据库模糊查询语句

    sql server数据库模糊查询语句   确切匹配: select * from hs_user where ID=123 模糊查询 select * from hs_user where ID l ...

  6. SQL Server数据库————模糊查询和聚合函数

    ***********模糊查询*********/ 关键字: like (!!!!字符串类型) in (,,)  匹配()内的某个具体值(括号里可以写多个值) between... and.. 在某两 ...

  7. WPF中ComboBox绑定数据库自动读取产生数据

    前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...

  8. easyui combobox实现本地模糊查询

    直接上代码 $("#combobox1").combobox({ valueField : "value", textField : "text&qu ...

  9. combobox实现模糊查询自动填充

    利用winform设计软件界面时,经常用到combobox控件,但有时需要绑定数据表中的数据,更进一步,需要实现对数据表中数据的模糊查询功能.本文就讲讲述如何用C#实现combobox下拉列表的模糊查 ...

随机推荐

  1. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  2. RecyleView

    RecyclerView是经典的ListView的进化与升华,它比ListView更加灵活,但也因此引入了一定的复杂性. 我们知道,ListView通过使用ViewHolder来提升性能.ViewHo ...

  3. VR应用里面的Photogrammetry技术是什么

    http://www.manew.com/thread-49556-1-1.html 具体使用 http://www.didayin.com/archives/632 软件下载 http://labs ...

  4. examine self thrice a day2016

    ----------------------------2016-----------------------  12.31.2016 2016年,感恩一路帮助过我的所有人!       每个人来到世 ...

  5. robot API笔记2

    robot.conf 设计方案 实现设置测试执行和输出处理. 这个方案实现了 RobotSettings 和 RebotSettings 内部使用的类 该框架. 不应该有这些类需要使用外部.这个包可以 ...

  6. NodeJS几个值得学习的开源框架

    1.Primus Primus,是Transformer的创造者,并且也被称为通用包装器实时框架.Primus里包含了大量的用于Node.js的实时框架,并 且它们都拥有各种不同的实时功能.此外,Pr ...

  7. css 描述css reset的作用和用途。

    描述css reset的作用和用途. 作用 : 因为浏览器的品种很多,每个浏览器的默认样式也是不同的.通过重新定义标签样式.“覆盖”浏览器的CSS默认属性. 有最简单的*{margin:0 ;  pa ...

  8. HTML5自学笔记[ 23 ]canvas绘图基础7

    变换矩阵: transform(a,b,c,d,e,f),多次使用该函数,效果是多次状态改变的累加: setTransform(a,b,c,d,e,f),会忽略之前的变换,从起始状态开始改变.

  9. C实现多线程

    #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <iostream& ...

  10. php 正则匹配中文

    在javascript中,要判断字符串是中文是很简单的.比如:var str = "php编程";if (/^[\u4e00-\u9fa5]+$/.test(str)) {aler ...