comboBox绑定数据库、模糊查询
实现:
一、绑定数据库
点击查询按钮,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绑定数据库、模糊查询的更多相关文章
- silverlight中 ComboBox绑定数据库,并获取当前选定值
silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...
- 老生常谈combobox和combotree模糊查询
FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...
- python 操作mongodb数据库模糊查询
# -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...
- ext.net中ComboBox空间实现模糊查询
ComboBox中的属性添加Mode="Local"可以实现第一个字的模糊查询但是搜索中间的字无法实现 现提供一下方法使用正则表达式实现全模糊查询 <ext:ComboBox ...
- 2019-2-14sql server数据库模糊查询语句
sql server数据库模糊查询语句 确切匹配: select * from hs_user where ID=123 模糊查询 select * from hs_user where ID l ...
- SQL Server数据库————模糊查询和聚合函数
***********模糊查询*********/ 关键字: like (!!!!字符串类型) in (,,) 匹配()内的某个具体值(括号里可以写多个值) between... and.. 在某两 ...
- WPF中ComboBox绑定数据库自动读取产生数据
前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...
- easyui combobox实现本地模糊查询
直接上代码 $("#combobox1").combobox({ valueField : "value", textField : "text&qu ...
- combobox实现模糊查询自动填充
利用winform设计软件界面时,经常用到combobox控件,但有时需要绑定数据表中的数据,更进一步,需要实现对数据表中数据的模糊查询功能.本文就讲讲述如何用C#实现combobox下拉列表的模糊查 ...
随机推荐
- Python学习笔记1—模块
模块的使用 引用模块的两种形式 形式一: import module_name 形式二: from module1 import module11 (module11是module的子模块) 例: ...
- iOS开发之 获取手机的网络的ip地址
首先在使用的地方导入 #include <ifaddrs.h> #include <arpa/inet.h> 然后直接调用 - (NSString *)getIPAddress ...
- fibonacci 斐波那契数列
1.小兔子繁殖问题 (有该问题的详细来由介绍) 2.台阶问题 题目:一个人上台阶可以一次上一个或者两个,问这个人上n层的台阶,一共有多少种走法. 递归的思路设计模型: i(台阶阶数) ...
- 别人要访问我的电脑上部署的tomcat,必须关闭防火墙吗?
局域网内要访问服务器上部署的tomcat,必须关闭防火墙吗? 不一定. 如果是需要使用 IP:端口号(ip:port)来访问,可以做以下设置(这里仅是说的tomcat访问). 首先在服务器的控制面板中 ...
- Sqlerver_各类函数
SQL Aggregate 函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. 有用的 Aggregate 函数: AVG() - 返回平均值-SELECT AVG(colu ...
- 原!!tomcat7.0 配置数据库连接池 SQLServer2000
SQLServer2000所需的3个驱动jar包 msbase.jarmssqlserver.jarmsutil.jar 放入WEB-INF lib文件夹中 1.META-INF 创建一个contex ...
- c++ 对象内存分配和虚函数
1. c++类对象(不含虚函数)在内存中的分布 c++类中有四种成员:静态数据.非静态数据.静态函数.非静态函数. 1. 非静态数据成员放在每个对象内部,作为对象专有的数据成员 2. 静态数据成员被抽 ...
- noip2016酱油记day1
真的是noip2016酱油记了. t1模拟,应该可以过. t2用了个简单的桶瞎搞,估计剩50pt了. t3直接不会写. 心好累... 考的分数肯定没去年高. 但不论如何,明天正常发挥就好. 正常发挥下 ...
- python 练习 27
ython continue 语句跳出本次循环,而break跳出整个循环. continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环. continue语句用在whil ...
- 添加SSH密钥到GitHub
$ clip < ~/.ssh/id_rsa.pubbash: /c/Users/UsersName/.ssh/id_rsa.pub: No such file or directory [转] ...